Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android fragment manager fragment change listener

Tags:

android

When replacing a fragment using a FragmentManager is it possible to add a listener for when the event is completed?

So

myFragmentManager
            .beginTransaction()
            .replace(R.id.fragmentContainer, fragment, FRAGMENT_TAG)
            .commit()

can I add a listener to myFragmentManager so that I know when the fragment has been replaced?

like image 626
Rgfvfk Iff Avatar asked May 06 '18 05:05

Rgfvfk Iff


People also ask

What is FragmentManager in Android?

FragmentManager is the class responsible for performing actions on your app's fragments, such as adding, removing, or replacing them, and adding them to the back stack.

How do I return one fragment to another?

The title of the toolbar should change between the two fragments (from "First Fragment" to "Second Fragment"). The second fragment should have a "back button" (arrow left icon) in the toolbar that, on click, lets the user go back to the first fragment.

How do you communicate two fragments?

We can communicate within fragments using the ViewModel. We can also communicate between fragments using Interface.


2 Answers

FragmentManager (android.support.v4.app.FragmentManager) has a nice method called registerFragmentLifecycleCallbacks which helps register a FragmentManager.FragmentLifecycleCallbacks. This allows us to take actions during the fragment's life cycle. You can do something in onFragmentCreated() as you wish.

FragmentManager fragmentManager =  getSupportFragmentManager();
        fragmentManager.registerFragmentLifecycleCallbacks(new FragmentManager.FragmentLifecycleCallbacks() {
            @Override
            public void onFragmentPreAttached(FragmentManager fm, Fragment f, Context context) {
                super.onFragmentPreAttached(fm, f, context);
                Log.v("FragXX1", f.getTag());
            }

            @Override
            public void onFragmentAttached(FragmentManager fm, Fragment f, Context context) {
                super.onFragmentAttached(fm, f, context);
                Log.v("FragXX2", f.getTag());
            }

            @Override
            public void onFragmentCreated(FragmentManager fm, Fragment f, Bundle savedInstanceState) {
                super.onFragmentCreated(fm, f, savedInstanceState);
                Log.v("FragXX3", f.getTag());
            }

            @Override
            public void onFragmentActivityCreated(FragmentManager fm, Fragment f, Bundle savedInstanceState) {
                super.onFragmentActivityCreated(fm, f, savedInstanceState);
                Log.v("FragXX4", f.getTag());
            }

            @Override
            public void onFragmentViewCreated(FragmentManager fm, Fragment f, View v, Bundle savedInstanceState) {
                super.onFragmentViewCreated(fm, f, v, savedInstanceState);
                Log.v("FragXX5", f.getTag());
            }

            @Override
            public void onFragmentStarted(FragmentManager fm, Fragment f) {
                super.onFragmentStarted(fm, f);
                Log.v("FragXX6", f.getTag());
            }

            @Override
            public void onFragmentResumed(FragmentManager fm, Fragment f) {
                super.onFragmentResumed(fm, f);
                Log.v("FragXX7", f.getTag());
            }

            @Override
            public void onFragmentPaused(FragmentManager fm, Fragment f) {
                super.onFragmentPaused(fm, f);
                Log.v("FragXX8", f.getTag());
            }

            @Override
            public void onFragmentStopped(FragmentManager fm, Fragment f) {
                super.onFragmentStopped(fm, f);
                Log.v("FragXX9", f.getTag());
            }

            @Override
            public void onFragmentSaveInstanceState(FragmentManager fm, Fragment f, Bundle outState) {
                super.onFragmentSaveInstanceState(fm, f, outState);
                Log.v("FragXX10", f.getTag());
            }

            @Override
            public void onFragmentViewDestroyed(FragmentManager fm, Fragment f) {
                super.onFragmentViewDestroyed(fm, f);
                Log.v("FragXX11", f.getTag());
            }

            @Override
            public void onFragmentDestroyed(FragmentManager fm, Fragment f) {
                super.onFragmentDestroyed(fm, f);
                Log.v("FragXX12", f.getTag());
            }

            @Override
            public void onFragmentDetached(FragmentManager fm, Fragment f) {
                super.onFragmentDetached(fm, f);
                Log.v("FragXX13", f.getTag());
            }
        }, true);

        FragmentTransaction ft = fragmentManager.beginTransaction();
        ft.replace(R.id.container, new TradeFragment(), "trien");
        ft.commit();
like image 186
Fazan Cheng Avatar answered Sep 23 '22 00:09

Fazan Cheng


Your new Fragment's onResume function is launched when the new Fragment is active. You can do what you want to do when the previous Fragment is replaced in it.

like image 45
Zelig63 Avatar answered Sep 20 '22 00:09

Zelig63