当前位置:网站首页>How to determine fragment restored from Backstack

How to determine fragment restored from Backstack

2022-06-12 20:09:00 Superior virtue and weak water

problem :

Been searching for this issue for a while to no avail now: I've been looking for this problem, but it doesn't help now :

How to determine fragment is being restored from backstack? How to determine if a clip is from backstack recovery ?I'm using the compatibility library and a ListFragment inside a FragmentActivity. I am here FragmentActivity Compatibility libraries and ListFragment.When an item inside ListFragment is selected, a new Fragment is started to replace the ListFragment. choice ListFragment Project in , A new fragment will be launched to replace ListFragment.

I noticed that when the FragmentActivity gets paused, the Fragment's onSaveInstanceState is called. I noticed when FragmentActivity When suspended , Would call Fragment Of onSaveInstanceState.But when the Fragment is put into the back stack via FragmentTransaction, onSaveInstanceState doesn't get called, then the lifecycle methods onCreateView and onActivityCreated gets called with null savedInstanceState Bundle. But when Fragment adopt FragmentTransaction When put into the background stack ,onSaveInstanceState Will not be called , So use null savedInstanceState Bundle Call lifecycle method onCreateView and onActivityCreated.

I'm asking this because I want to load some data when the Fragment is created or restored, but not so when user comes back via. I ask this because I want to create or restore Fragment Load some data , But this is not the case when the user comes back through it .backstack. In the stack .

I've looked at How to check if Fragment was restored from a backstack? I have ever seen How to check whether a fragment is recovered from the background ?but want to add more details in hopes this would incite an answer. But I hope to add more details , Hopefully this will lead to the answer .

Edit: just noticed http://developer.android.com/reference/android/app/Fragment.html#onSaveInstanceState(android.os.Bundle) says edit : Just noticed http://developer.android.com/reference/android/app/Fragment.html#onSaveInstanceState(android.os.Bundle) say

Note however: this method may be called at any time before onDestroy(). But please pay attention to : Can be in onDestroy() Call this method any time before .There are many situations where a fragment may be mostly torn down (such as when placed on the back stack with no UI showing), but its state will not be saved until its owning activity actually needs to save its state. in many instances , Most of the fragments may have been removed ( For example, placed in a place that does not display UI On the background stack of ), But before the activity it owns actually needs to save its state , Its state will not be saved .

So onSaveInstanceState is definitely out of the question... therefore onSaveInstanceState Absolutely impossible ......


Solution :

Reference resources : https://stackoom.com/en/question/XEGn
原网站

版权声明
本文为[Superior virtue and weak water]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202281512279676.html