In Android, if you have the Application context you can register an Application.ActivityLifecycleCallbacks
instance that will be called everytime an Activity goes through one of its lifecycle callbacks.
How can I accomplish the same for fragments? I think there is no such interface for Fragments nor any clear place where I would add that.
Maybe customizing a FragmentHostCallback
creating a FragmentController
but how can I plug that for the whole application?
The use case is a library that needs to be notified everytime a Fragment calls its lifecycle callbacks and I don't want to create a BaseFragment. I want to be called only from Application's onCreate and that's it (if possible...).
EDIT:
I've created an issue in Android Open Source Project about this.
A fragment life cycle is closely related to the lifecycle of its host activity which means when the activity is in the pause state, all the fragments available in the activity will also stop. Fragments added to the Android API in Android 3.0 which API version 11 to support flexible UI on large screens.
onDestroy()onDestroy() called to do final clean up of the fragment's state but Not guaranteed to be called by the Android platform.
A fragment can be used in multiple activities. Fragment life cycle is closely related to the life cycle of its host activity which means when the activity is paused, all the fragments available in the activity will also be stopped. A fragment can implement a behaviour that has no user interface component.
Since version 25.2.0 of Android support library, the class FragmentManager.FragmentLifecycleCallbacks
is static and accessible to all.
We can now use an instance of that class and register it in the supportFragmentManager
of the Activity
.
public class ExampleActivity extends AppCompatActivity { public void onCreate(Bundle savedInstaceState) { // initialization code getSupportFragmentManager() .registerFragmentLifecycleCallbacks(new FragmentManager.FragmentLifecycleCallbacks() { @Override public void onFragmentPreAttached(FragmentManager fm, Fragment f, Context context) { super.onFragmentPreAttached(fm, f, context); } @Override public void onFragmentAttached(FragmentManager fm, Fragment f, Context context) { super.onFragmentAttached(fm, f, context); } @Override public void onFragmentCreated(FragmentManager fm, Fragment f, Bundle savedInstanceState) { super.onFragmentCreated(fm, f, savedInstanceState); } @Override public void onFragmentActivityCreated(FragmentManager fm, Fragment f, Bundle savedInstanceState) { super.onFragmentActivityCreated(fm, f, savedInstanceState); } @Override public void onFragmentViewCreated(FragmentManager fm, Fragment f, View v, Bundle savedInstanceState) { super.onFragmentViewCreated(fm, f, v, savedInstanceState); } @Override public void onFragmentStarted(FragmentManager fm, Fragment f) { super.onFragmentStarted(fm, f); } @Override public void onFragmentResumed(FragmentManager fm, Fragment f) { super.onFragmentResumed(fm, f); } @Override public void onFragmentPaused(FragmentManager fm, Fragment f) { super.onFragmentPaused(fm, f); } @Override public void onFragmentStopped(FragmentManager fm, Fragment f) { super.onFragmentStopped(fm, f); } @Override public void onFragmentSaveInstanceState(FragmentManager fm, Fragment f, Bundle outState) { super.onFragmentSaveInstanceState(fm, f, outState); } @Override public void onFragmentViewDestroyed(FragmentManager fm, Fragment f) { super.onFragmentViewDestroyed(fm, f); } @Override public void onFragmentDestroyed(FragmentManager fm, Fragment f) { super.onFragmentDestroyed(fm, f); } @Override public void onFragmentDetached(FragmentManager fm, Fragment f) { super.onFragmentDetached(fm, f); } }, true); } }
Well the activity dispatches its callbacks for the FragmentManager. Later the fragment manager dispatch the calls to the Fragment. There is no place to register the FragmentLifeCycleCallback in the FragmentManager. However at anytime after the fragment is initialized u can get the Fragment state from mState variable in the Fragment. you can only track the backstack in the fragment manager as below :
final FragmentManager fragmentManager = getSupportFragmentManager(); fragmentManager.addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() { @Override public void onBackStackChanged() { Log.i("BACKSTACK","Count" +fragmentManager.getBackStackEntryCount()); } });
hope this will help.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With