public void navigationToFragment(Fragment fragment, Bundle bundle, String strTitle){ String fragmentName = fragment.getClass().getName(); FragmentManager manager = getSupportFragmentManager(); boolean isFragmentPop = manager.popBackStackImmediate (fragmentName, 0); FragmentTransaction ft = manager.beginTransaction(); if (isFragmentPop) fragment=manager.findFragmentByTag(fragmentName); if (bundle!=null) fragment.setArguments(bundle); if (strTitle!=null) getSupportActionBar().setTitle(strTitle); ft.replace(R.id.container,fragment,fragmentName); ft.addToBackStack(fragmentName); ft.commit(); } 

navigationToFragment(new AlbumsGroupFragment(),bundle,model.name); This is how the method is called.

The problem is that if Fragment A is created, and then Fragment B, then after calling onBackPressed, Fragment A calls OnCreateView.

And I wanted the fragment A not to be created, but as if it came out of the stack. And then some kind of fiction is obtained

  • I would be grateful for any ideas - user239760

1 answer 1

You need to prescribe good logic in the fragment in the onCreateView method, this cannot be avoided. For example, you have some CheckBox and you need to save its state. For this there is a method `onSaveInstanseState"

 @Override public void onSaveInstanseState(Bundle outState) { outState.putExtra("checked", checkBox.isChecked()); super.onSaveInstanseState(outState); } 

Now in your onCreateView you need to do this

 if(savedInstanceState != null) { checkBox.setChecked(savedInstanceState.getBooleanExtra("checked")); } 

Thus, upon leaving the stack, the checkbox data will be saved in a bundle and will be set back on the fragment upon return. I’ll say right away that if there are problems with animation, then you have to do unthinkable hacks, sometimes it comes to putting libraries into modules and creating methods for opening / closing something without animation