Call OnResume when Back Button Pressed when using

2019-02-18 19:52发布

问题:

I add first fragment 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 second fragment as

            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 press back button from AboutFragment (Second) then how to call a method of HomeFragment (First)

            onResume() is not fired which is creating problem

回答1:

I am posting late answer, but i think this will be useful for others

While we are using add() with Fragment transactions, onResume() will not be called on Source fragment. Only Destination fragment will get onResume() callback

But using this way, we can achieve onResume() callback to Source fragment :

Add this in Host Activity's onBackPressed()

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

Note : Be sure that you have called addToBackStack() with commit()

This code will give onResume() callback to Source fragment

Thank you



回答2:

instead of calling "add" for the second fragment use "replace instead"

i.e. instead of

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

use

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

So whenever u will press back button to go previous fragment, the first fragment's lifecycle will be called back again.

OR if it doesnt meet ur requirement, u 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();
}
}


回答3:

i am using this method for all my fragment a

// 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();

        }
    }


回答4:

If you are adding fragment

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

then it'll not call any method of first fragment on back press.

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

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

But this will call onCreateView() first then other methods.It is not possible in fragment on backpressed to call onResume(), as in activity happens