How to check if Fragment was restored from a backs

2019-04-07 13:59发布

问题:

Is that possible to check if Fragment was restored from a backstack? For example, if I had one, then replaced by another and back to previous by tapping "back" android button?

回答1:

You need to have your own backstack and keep track of it.

Look at this: http://adblogcat.com/fragment-transition-animations-while-hiding-the-previous-fragment/

The code holds its own stack of fragments and hides them instead of removing them, giving it the same effect as replace (with backstack) except it doesn't recreate the fragment every time so the state of the fragment is always intact.

However, this code has a memory leak and I have to update it with the latest where the fragments don't persist. Hope this helps.