Fragment lifecycle events in LifeCycleAware Fragment
Asked Answered
P

4

21

I have a lifecycle aware fragment and a LifecycleObserver class

        public class MyFragment extends Fragment {
            @Override
            public void onCreate(@Nullable Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                new MyObserver(this);

            }

            @Nullable
            @Override
            public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
                return inflater.inflate(R.layout.activity_main, container, false);
            }
        }

Following is my Observer class where it logs all the fragment events propery

        public class MyObserver implements LifecycleObserver {
            private static final String TAG = "MyObserver";
            public MyObserver(LifecycleOwner lifecycleOwner) {
                lifecycleOwner.getLifecycle().addObserver(this);
            }

            @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
            public void onCreate(){
                Log.d(TAG, "onCreate: ");
            }

            @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
            public void onPause(){
                Log.d(TAG, "onPause: ");
            }

            @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
            public void onDestroy(){
                Log.d(TAG, "onDestroy: ");
            }

            @OnLifecycleEvent(Lifecycle.Event.ON_START)
            public void onStart(){
                Log.d(TAG, "onStart: ");
            }

        }

I want to listen to fragment specific lifecycle events like onDestroyView and onActivityCreated but these events are not there in

Lifecycle.Event. It contains only activity events. How can I listen for fragment events in my observer

Poundfoolish answered 3/11, 2017 at 9:0 Comment(3)
Were you able to find anything about this?Yeah
I am also trying to figure this out. Did you find a solution?Wieren
Any way to achieve this using a lifecycle observer? The answers below can be implemented without lifecycle observer by writing our own custom methods and calling from fragments.Cholera
R
16

You can observe the fragment's viewLifecycleOwner lifecycle.

viewLifecycleOwner.lifecycle.addObserver(yourObserverHere)

Then the fragment's onDestroyView lifecyle method is tied to the @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) annotated method.

Note that the fragment's viewLifecycleOwner is only available between onCreateView and onDestroyView lifecycle methods.

Rovner answered 8/2, 2019 at 2:23 Comment(5)
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) called when the fragment is destroyed not in destroyviewMultiplier
@Multiplier it depends on which lifecycle, the fragment lifecycle or the fragment's view lifecycle.Rovner
Needs clarificationMultiplier
@Multiplier Please read about getViewLifecycleOwner() method and you will understand it.Rovner
@Multiplier check my answer for more details regarding ON_DESTROY Incandescence
I
1

Just a supplement to Basim Alamuddin's answer:

As you can see in the resource codes, @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) is also called when you add an observer to viewLifecycleOwner: viewLifecycleOwner.lifecycle.addObserver(this)

// androidx.fragment.app.Fragment

void performDestroyView() {
    if (mView != null) {
        mViewLifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY);
    }
    onDestroyView();

void performDestroy() {
    mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY);
    onDestroy();
Incandescence answered 12/10, 2019 at 11:57 Comment(0)
B
-5

Well, according to this, literally onDestroyView will be called after onStop. So, If you need the logic to run before onDestroyView or detecting it, i think it's is best to call it or detect in the fragment's onStop() method. In your case, i think you need to implement Lifecycle.Event.ON_STOP & Lifecycle.Event.ON_START for onActivityCreated.

Hope it helps you.

Bucaramanga answered 3/11, 2017 at 9:30 Comment(1)
developer.android.com/guide/fragments/lifecycleQuaky
P
-6

Fragment Life Cycle

onCreateView()

Called to create the view hierarchy associated with the fragment.

onDestroyView()

Called when the view hierarchy associated with the fragment is being removed.

Plexor answered 3/11, 2017 at 9:24 Comment(1)
My question is about Architecture ComponentsPoundfoolish

© 2022 - 2024 — McMap. All rights reserved.