I'm using this line :
getSupportFragmentManager().popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
in a method inside activity which is accessed through Fragment to clear the whole backstack from the supportFragmentManager
. But I got an error saying java.lang.IllegalStateException: Fragment no longer exists for key f0: index 0
.
This is how I replace Fragments :
public void replaceFragment(final Fragment fragment, final String tag){
new Handler().post(new Runnable() {
@Override
public void run() {
manager.beginTransaction()
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.replace(R.id.activity_newsfeed_frame, fragment, tag)
.addToBackStack(tag)
.setAllowOptimization(false)
.commit();
manager.executePendingTransactions();
}
});
}
Where did I go wrong here??
Update :
Tried using try catch block
try{
getSupportFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
} catch (Exception e){
// TODO
}
But it doesn't work.