Added Android code
[wl-app.git] / Android / app / src / main / java / com / moiseum / wolnelektury / base / mvp / PresenterFragment.java
1 package com.moiseum.wolnelektury.base.mvp;
2
3 import android.os.Bundle;
4 import android.support.annotation.NonNull;
5 import android.view.View;
6
7 import com.moiseum.wolnelektury.base.AbstractFragment;
8
9
10 /**
11  * Fragment that creates {@link LifecyclePresenter} and in its lifecycle methods calls corresponding methods of
12  * presenter.
13  *
14  * @param <P> type of presenter for this fragment.
15  */
16 public abstract class PresenterFragment<P extends FragmentPresenter> extends AbstractFragment {
17
18         private P presenter;
19
20         protected abstract P createPresenter();
21
22         protected P getPresenter() {
23                 return presenter;
24         }
25
26         @Override
27         public void onCreate(Bundle savedInstanceState) {
28                 super.onCreate(savedInstanceState);
29                 presenter = createPresenter();
30                 presenter.onCreate(savedInstanceState);
31         }
32
33         @Override
34         public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
35                 super.onViewCreated(view, savedInstanceState);
36                 presenter.onViewCreated(savedInstanceState);
37         }
38
39         @Override
40         public void onStart() {
41                 super.onStart();
42                 presenter.onStart();
43         }
44
45         @Override
46         public void onStop() {
47                 super.onStop();
48                 presenter.onStop();
49         }
50
51         @Override
52         public void onResume() {
53                 super.onResume();
54                 presenter.onResume();
55         }
56
57         @Override
58         public void onPause() {
59                 super.onPause();
60                 presenter.onPause();
61         }
62
63         @Override
64         public void onDestroyView() {
65                 super.onDestroyView();
66                 presenter.onDestroyView();
67         }
68
69         @Override
70         public void onDestroy() {
71                 super.onDestroy();
72                 presenter.onDestroy();
73         }
74
75         @Override
76         public void onSaveInstanceState(@NonNull Bundle outState) {
77                 super.onSaveInstanceState(outState);
78                 presenter.onSaveInstanceState(outState);
79         }
80 }