I have seen this post but the solutions posted here are not working for me: get the latest fragment in backstack
When I replace a fragment for another I use:
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
ft.replace(android.R.id.tabcontent, fragment, tag);
ft.addToBackStack(null);
ft.commit();
So, please notice that I'm using tags to detect any fragment.
My problem in particular is the following:
Suppose I have fragment_A, fragment_B and fragment_C. I can get to fragment_C from fragment_A or fragment_B and depending of the parent of call I have to do something particular. So, again I need to recover the last FRAGMENT in the back stack.
When I try to do:
FragmentManager fm = getSupportFragmentManager();
for(int entry = 0; entry < fm.getBackStackEntryCount(); entry++){
String ide = fm.getBackStackEntryAt(entry).getName();
Log.i("TAG", "Found fragment: " + ide);
}
I get nulls. If I use getId() instead, I get numbers so I tried doing:
int id = fm.getBackStackEntryAt(entry).getId();
Fragment fragment = fm.findFragmentById(id);
Log.i("TAG", "Found fragment: " + fragment.getTag());
But I get nulls.. I dont know what else to do, so any help will be appreciated.
Edit: I call this methods in onBackPressed() { ...}
This means that the transaction will be remembered after it is committed, and will reverse its operation when later popped off the stack. Parameters name An optional name for this back stack state, or null.
The user can later reverse the transaction and bring back the previous fragment by pressing the Back button. If you added or removed multiple fragments within a single transaction, all of those operations are undone when the back stack is popped.
If you're using BackStackEntry.getName()
, it should be:
ft.replace(android.R.id.tabcontent, fragment, tag);
ft.addToBackStack(tag);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With