Skip to content

Commit 8ffc4c8

Browse files
author
roman_tcaregorodtcev
committed
MvpBottomSheetDialogFragment added
1 parent 37cca64 commit 8ffc4c8

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package com.omegar.mvp;
2+
3+
import android.os.Build;
4+
import android.os.Bundle;
5+
6+
import androidx.fragment.app.Fragment;
7+
8+
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
9+
10+
public class MvpBottomSheetDialogFragment extends BottomSheetDialogFragment {
11+
12+
private boolean mIsStateSaved;
13+
private MvpDelegate<? extends MvpBottomSheetDialogFragment> mMvpDelegate;
14+
15+
public void onCreate(Bundle savedInstanceState) {
16+
super.onCreate(savedInstanceState);
17+
18+
getMvpDelegate().onCreate(savedInstanceState);
19+
}
20+
21+
public void onResume() {
22+
super.onResume();
23+
24+
mIsStateSaved = false;
25+
26+
getMvpDelegate().onAttach();
27+
}
28+
29+
public void onSaveInstanceState(Bundle outState) {
30+
super.onSaveInstanceState(outState);
31+
32+
mIsStateSaved = true;
33+
34+
getMvpDelegate().onSaveInstanceState(outState);
35+
getMvpDelegate().onDetach();
36+
}
37+
38+
@Override
39+
public void onStop() {
40+
super.onStop();
41+
42+
getMvpDelegate().onDetach();
43+
}
44+
45+
@Override
46+
public void onDestroyView() {
47+
super.onDestroyView();
48+
49+
getMvpDelegate().onDetach();
50+
getMvpDelegate().onDestroyView();
51+
}
52+
53+
@Override
54+
public void onDestroy() {
55+
super.onDestroy();
56+
57+
//We leave the screen and respectively all fragments will be destroyed
58+
if (getActivity().isFinishing()) {
59+
getMvpDelegate().onDestroy();
60+
return;
61+
}
62+
63+
// When we rotate device isRemoving() return true for fragment placed in backstack
64+
// http://stackoverflow.com/questions/34649126/fragment-back-stack-and-isremoving
65+
if (mIsStateSaved) {
66+
mIsStateSaved = false;
67+
return;
68+
}
69+
70+
// See https://github.com/Arello-Mobile/Moxy/issues/24
71+
boolean anyParentIsRemoving = false;
72+
73+
if (Build.VERSION.SDK_INT >= 17) {
74+
Fragment parent = getParentFragment();
75+
while (!anyParentIsRemoving && parent != null) {
76+
anyParentIsRemoving = parent.isRemoving();
77+
parent = parent.getParentFragment();
78+
}
79+
}
80+
81+
if (isRemoving() || anyParentIsRemoving) {
82+
getMvpDelegate().onDestroy();
83+
}
84+
}
85+
86+
/**
87+
* @return The {@link MvpDelegate} being used by this Fragment.
88+
*/
89+
public MvpDelegate getMvpDelegate() {
90+
if (mMvpDelegate == null) {
91+
mMvpDelegate = new MvpDelegate<>(this);
92+
}
93+
94+
return mMvpDelegate;
95+
}
96+
97+
}

0 commit comments

Comments
 (0)