Calling OnResume when the back button is pressed while using fragments

I am adding the first snippet like this

            FragmentManager fm = SliderActivity.this
                    .getSupportFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            Fragment fragment = null;
            fragment = new HomeFragment();
            ft.add(R.id.content_fragment, fragment, "Home");
                ft.addToBackStack("Home");
                ft.commit();

      

and the second snippet

            FragmentManager fm = SliderActivity.this
                    .getSupportFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            Fragment fragment = null;
            fragment = new HomeFragment();
            ft.add(R.id.content_fragment, fragment, "About");
            ft.addToBackStack("About");
            ft.commit();

      

My question is, if I click the back button from AboutFragment (Second), then how do I call the HomeFragment (First) method

            onResume() is not fired which is creating problem

      

+3


source to share


4 answers


I am posting a late answer, but I think it will be helpful to others.

As long as we use a add()

Fragment with transactions, the SourceonResume()

will not be called in the Fragment . Fragment only Destination will receive a callbackonResume()

But using this way, we can achieve a callback onResume()

for the Source fragment :

Add this to Host Activity onBackPressed()



@Override
public void onBackPressed() {
    getSupportFragmentManager().getFragments()
                .get(getSupportFragmentManager().getBackStackEntryCount() - 1).onResume();
    super.onBackPressed();
}

      

Note. Make sure you called addToBackStack()

withcommit()

This code will give a onResume()

callback Source snippet

thank

+5


source


I am using this method for my whole fragment



// For the Fragment Replace And AddtobackStack
    void replaceFragment(Fragment fragment) {
        String backStateName = fragment.getClass().getName();
        String fragmentTag = backStateName;

        FragmentManager manager = this.getSupportFragmentManager();
        boolean fragmentPopped = manager
                .popBackStackImmediate(backStateName, 0);

        if (!fragmentPopped && manager.findFragmentByTag(fragmentTag) == null) {
            // fragment not in back stack, create it.
            FragmentTransaction ft = manager.beginTransaction();
            ft.replace(R.id.container, fragment, fragmentTag);
            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);

            ft.addToBackStack(backStateName);
            ft.commit();

        }
    }

      

0


source


instead of calling "add" for the second snippet, use "replace instead of"

i.e. instead

ft.add(R.id.content_fragment, fragment, "Home");

      

using

ft.replace(R.id.content_fragment, fragment, "Home");

      

Therefore, whenever u presses the Back button to go to the previous chunk, the first loop of the chunk will be called again.

OR if it doesn't meet the ear requirements, you can do this:

private final static String TAG_FRAGMENT = "TAG_FRAGMENT";

private void showFragment() {
final Myfragment fragment = new MyFragment();
final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment, fragment, TAG_FRAGMENT);
transaction.addToBackStack(null);
transaction.commit();
}

@Override
public void onBackPressed() {
final Myfragment fragment = (Myfragment) getSupportFragmentManager().findFragmentByTag(TAG_FRAGMENT);

if (fragment.allowBackPressed()) { // and then you define a method allowBackPressed with the logic to allow back pressed or not
    super.onBackPressed();
}
}

      

0


source


If you add snippet

ft.add(R.id.content_fragment, fragment, "About");

      

then it will not call any method of the first fragment when pressed back.

To call the method of the first fragment, replace the fragment with the following:

ft.replace(R.id.content_fragment, fragment, "About");

      

But this will call the onCreateView()

other methods first and then. This is not possible in the clicked-to-call fragment onResume()

as in the activity case

0


source







All Articles