Android 使用setRetainInstance(true)保留片段实例是如何工作的?
我有一个没有视图的片段,用于承载异步任务。我希望在配置更改(例如方向更改)期间保留片段对象。据我所知,在片段的onCreate()方法中使用setRetainInstance(true)就足够了。但我的问题是: 首先,让我发布一些我正在使用的代码 “我的活动”在其onCreate()方法中使用此选项 下面是片段代码的一些示例Android 使用setRetainInstance(true)保留片段实例是如何工作的?,android,android-fragments,Android,Android Fragments,我有一个没有视图的片段,用于承载异步任务。我希望在配置更改(例如方向更改)期间保留片段对象。据我所知,在片段的onCreate()方法中使用setRetainInstance(true)就足够了。但我的问题是: 首先,让我发布一些我正在使用的代码 “我的活动”在其onCreate()方法中使用此选项 下面是片段代码的一些示例 class DirectionsFetcherFragment extends Fragment { /* string constants omitted */
class DirectionsFetcherFragment extends Fragment
{
/* string constants omitted */
public static DirectionsFetcherFragment newInstance(/*omitted*/)
{
DirectionsFetcherFragment instance = new DirectionsFetcherFragment();
Bundle data = new Bundle();
/* omitted - adding the necessary data to the bundle */
instance.setArguments(data);
return instance;
}
public DirectionsFetcherFragment()
{
super();
/* omitted - setting default values for member variables */
}
@Override
public void onCreate(Bundle savedInstance)
{
Log.wtf(TAG, "onCreate()");
super.onCreate(savedInstance);
setRetainInstance(true);
Bundle arguments = getArguments();
/* omitted - other initializations tasks that take place */
}
}
下面是正在发生的事情。在初始活动创建时,fetcher引用如预期的那样为null,因此我创建了一个新片段并通过事务添加它。但是,当/如果我旋转设备,当活动的onCreate()方法运行时,fetcher引用为null,findFragmentByTag()不会返回任何片段。在LogCat中,我可以清楚地看到片段的onDetach()->onAttach()方法是为第一个片段运行的,但是由于某种原因,该片段随后被垃圾收集,并创建了一个新片段
以下是LogCat输出:
D/DirectionsViewer﹕ Initializing from intent <<-- Initial activity creation
D/DirectionsViewer﹕ Creating new DirectionsFetcherFragment instance <<-- Fragment creation
A/DirectionsFetcherFragment﹕ onAttach()
A/DirectionsFetcherFragment﹕ onCreate()
V/DirectionsViewer﹕ onStart()
V/DirectionsViewer﹕ onResume()
V/DirectionsViewer﹕ onSaveInstanceState() <<-- Rotationing and saving instance
A/DirectionsFetcherFragment﹕ onDetach() <<-- Fragment correctly detaches
D/DirectionsViewer﹕ Restoring from saved instance <<-- Activity restoration
D/DirectionsViewer﹕ Creating new DirectionsFetcherFragment instance <<-- Why is this happenning, shouldn't I get a reference to the existing fragment?
A/DirectionsFetcherFragment﹕ onAttach()
A/DirectionsFetcherFragment﹕ onCreate()
V/DirectionsViewer﹕ onStart()
V/DirectionsViewer﹕ onResume()
V/DirectionsViewer﹕ Deleting DirectionsViewer instance <<-- This is the old activity that gets garbage collected
A/DirectionsFetcherFragment﹕ Deleting DirectionsFetcherFragment instance <<-- This is the old fragment instance that gets garbage collected
D/DirectionsViewer﹕ 从意图初始化
D/DirectionsViewer﹕ Initializing from intent <<-- Initial activity creation
D/DirectionsViewer﹕ Creating new DirectionsFetcherFragment instance <<-- Fragment creation
A/DirectionsFetcherFragment﹕ onAttach()
A/DirectionsFetcherFragment﹕ onCreate()
V/DirectionsViewer﹕ onStart()
V/DirectionsViewer﹕ onResume()
V/DirectionsViewer﹕ onSaveInstanceState() <<-- Rotationing and saving instance
A/DirectionsFetcherFragment﹕ onDetach() <<-- Fragment correctly detaches
D/DirectionsViewer﹕ Restoring from saved instance <<-- Activity restoration
D/DirectionsViewer﹕ Creating new DirectionsFetcherFragment instance <<-- Why is this happenning, shouldn't I get a reference to the existing fragment?
A/DirectionsFetcherFragment﹕ onAttach()
A/DirectionsFetcherFragment﹕ onCreate()
V/DirectionsViewer﹕ onStart()
V/DirectionsViewer﹕ onResume()
V/DirectionsViewer﹕ Deleting DirectionsViewer instance <<-- This is the old activity that gets garbage collected
A/DirectionsFetcherFragment﹕ Deleting DirectionsFetcherFragment instance <<-- This is the old fragment instance that gets garbage collected