forked from Arello-Mobile/Moxy
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from Omega-R/feature/bottom_dialog
MvpBottomSheetDialogFragment added
- Loading branch information
Showing
1 changed file
with
97 additions
and
0 deletions.
There are no files selected for viewing
97 changes: 97 additions & 0 deletions
97
moxy-androidx/src/main/java/com/omegar/mvp/MvpBottomSheetDialogFragment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package com.omegar.mvp; | ||
|
||
import android.os.Build; | ||
import android.os.Bundle; | ||
|
||
import androidx.fragment.app.Fragment; | ||
|
||
import com.google.android.material.bottomsheet.BottomSheetDialogFragment; | ||
|
||
public class MvpBottomSheetDialogFragment extends BottomSheetDialogFragment { | ||
|
||
private boolean mIsStateSaved; | ||
private MvpDelegate<? extends MvpBottomSheetDialogFragment> mMvpDelegate; | ||
|
||
public void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
|
||
getMvpDelegate().onCreate(savedInstanceState); | ||
} | ||
|
||
public void onResume() { | ||
super.onResume(); | ||
|
||
mIsStateSaved = false; | ||
|
||
getMvpDelegate().onAttach(); | ||
} | ||
|
||
public void onSaveInstanceState(Bundle outState) { | ||
super.onSaveInstanceState(outState); | ||
|
||
mIsStateSaved = true; | ||
|
||
getMvpDelegate().onSaveInstanceState(outState); | ||
getMvpDelegate().onDetach(); | ||
} | ||
|
||
@Override | ||
public void onStop() { | ||
super.onStop(); | ||
|
||
getMvpDelegate().onDetach(); | ||
} | ||
|
||
@Override | ||
public void onDestroyView() { | ||
super.onDestroyView(); | ||
|
||
getMvpDelegate().onDetach(); | ||
getMvpDelegate().onDestroyView(); | ||
} | ||
|
||
@Override | ||
public void onDestroy() { | ||
super.onDestroy(); | ||
|
||
//We leave the screen and respectively all fragments will be destroyed | ||
if (getActivity().isFinishing()) { | ||
getMvpDelegate().onDestroy(); | ||
return; | ||
} | ||
|
||
// When we rotate device isRemoving() return true for fragment placed in backstack | ||
// http://stackoverflow.com/questions/34649126/fragment-back-stack-and-isremoving | ||
if (mIsStateSaved) { | ||
mIsStateSaved = false; | ||
return; | ||
} | ||
|
||
// See https://github.com/Arello-Mobile/Moxy/issues/24 | ||
boolean anyParentIsRemoving = false; | ||
|
||
if (Build.VERSION.SDK_INT >= 17) { | ||
Fragment parent = getParentFragment(); | ||
while (!anyParentIsRemoving && parent != null) { | ||
anyParentIsRemoving = parent.isRemoving(); | ||
parent = parent.getParentFragment(); | ||
} | ||
} | ||
|
||
if (isRemoving() || anyParentIsRemoving) { | ||
getMvpDelegate().onDestroy(); | ||
} | ||
} | ||
|
||
/** | ||
* @return The {@link MvpDelegate} being used by this Fragment. | ||
*/ | ||
public MvpDelegate getMvpDelegate() { | ||
if (mMvpDelegate == null) { | ||
mMvpDelegate = new MvpDelegate<>(this); | ||
} | ||
|
||
return mMvpDelegate; | ||
} | ||
|
||
} |