简体   繁体   中英

Do not remove fragment from backstack

I want to save fragments in manager, even if I click on back. This is code:

 FragmentTransaction fragmentTransaction = getSupportFragmentManager()
                .beginTransaction();
        fragmentTransaction.setCustomAnimations(R.anim.slide_in_right_frag, R.anim.slide_out_left_frag, R.anim.slide_in_left, R.anim.slide_out_right);

        FragmentManager manager = getSupportFragmentManager();
        manager.findFragmentByTag(title);

        if (manager.findFragmentByTag(title) == null) {
            fragmentTransaction.replace(R.id.fragment_container, fragment, title);
            if (addToBackStack) {
                fragmentTransaction.addToBackStack(null);
            }
            fragmentTransaction.commit();
        } else {
            fragmentTransaction.show(manager.findFragmentByTag(title)).commit();
        }

now if I click onBack my fragment is removed from back stack and manager. I want to even if I click back save this fragment so If I click next I want to back to this fragment. This is my way:

frag1 -> clickOnNext -> frag2 -> put some data in edittexts ->
clickOnBack -> frag1 -> clickOnNext -> frag2(frag has data from first time).

Now always get new Instance of fragment2 .

Use fragmentTransaction.add(R.id.fragment_container, fragment, title); instead of fragmentTransaction.replace(R.id.fragment_container, fragment, title);

And handle showing/hiding of fragments in your activity onBackPressed event.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM