Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hooking into fragment's lifecycle like Application.ActivityLifecycleCallbacks

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.

like image 287
Olinasc Avatar asked Sep 22 '15 17:09

Olinasc


People also ask

How Fragment life cycle is related with activity life cycle?

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.

Which method is called when called to do final clean up of the fragment's state but not guaranteed to be called by the Android platform?

onDestroy()onDestroy() called to do final clean up of the fragment's state but Not guaranteed to be called by the Android platform.

What is fragment and its life cycle?

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.


2 Answers

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);        } } 
like image 87
Olinasc Avatar answered Oct 12 '22 04:10

Olinasc


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.

like image 22
M.Khouli Avatar answered Oct 12 '22 04:10

M.Khouli