并不总是调用onDestroy().如果被调用,则只执行部分代码.
而且大部分时间在LogCat中我只看到消息"首先调用destroy的gps状态".这是为什么?
protected void onDestroy() {
super.onDestroy();
Log.d("on destroy called", "gps state on destroy called first");
editor.putBoolean("gpsOn", false);
Log.d("on destroy called", "gps state on destroy called second");
editor.commit();
Log.d("on destroy called", "gps state on destroy called third");
stopRouteTracking();
Log.d("on destroy called", "gps state on destroy called fourth");
}
Run Code Online (Sandbox Code Playgroud) 我的Android应用程序有一个ActionBar变化,Fragment占用了一定的FrameLayout.当我onSaveInstanceState更改选项卡时,我试图用来保存片段的状态,以便可以恢复它onCreateView.
问题是,onSaveInstanceState永远不会被称为.在Fragment的onDestroyView和onCreateView方法的调用,但Bundle供给onCreateView仍然是零.
有人可以向我解释onSaveInstanceState实际调用的时间,如何确保在切换选项卡时调用它,或者在Fragment分离和重新连接时保存和恢复状态的最佳实践?
分段:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.event_log, container, false);
// Retrieve saved state
if (savedInstanceState != null){
System.out.println("log retrieved");
} else {
System.out.println("log null");
}
return view;
}
@Override
public void onSaveInstanceState(Bundle outState) {
System.out.println("log saved");
super.onSaveInstanceState(outState);
// more code
}
Run Code Online (Sandbox Code Playgroud)
活动:
/** …Run Code Online (Sandbox Code Playgroud)