【发布时间】:2014-09-01 03:42:50
【问题描述】:
我很难理解片段生命周期与在后台堆栈中的片段之间切换的关系。如果我的问题暴露了多个误解,请多多包涵。
这是我的代码:
public class SomeFragment extends Fragment {
private SomeCustomView customView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.some_fragment, container, false);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Create the child view
customView = (SomeCustomView) getView().findViewById(R.id.some_fragment_child_view);
customView.initializeMyCustomView();
}
}
如您所见,我的片段有一个子视图。子视图是自定义视图。代码如下:
public class SomeCustomView extends SurfaceView implements SurfaceHolder.Callback {
private boolean aVariableWhichMustPersistForLifetimeOfApplication;
}
每当这个片段被添加到后台堆栈然后恢复,变量customView被重新创建,所以我失去了aVariableWhichMustPersistForLifetimeOfApplication的值。这给我带来了各种各样的问题。
应用程序开始使用Activity,它只显示SomeCustomView,并且没有片段。现在我必须添加功能,所以我将自定义视图变成了一个片段,因此我遇到了这个问题。
【问题讨论】:
标签: android android-fragments back-stack