屏幕旋转后未调用onSaveInstanceState [英] onSaveInstanceState is not getting called after screen rotation
问题描述
我知道onSaveInstanceState上有很多相关的问题,但我找不到解决问题的方法.
I know there are lots of quetions regarden onSaveInstanceState but I wasn't able to find an answer to my problem.
我有一个扩展AppCompatActivity的活动;此活动使用3个片段,它具有一个变量'int currentStep'来跟踪显示哪个片段.在onSavedInstanceState方法中,将currentStep变量存储在分发包中,然后在onCreate方法中将其还原.
I have an activity that extends AppCompatActivity; this activity uses 3 fragments and it has a variable 'int currentStep' to track which fragment is being displayed. In the onSavedInstanceState method I store the currentStep variable in the bundle, and then in onCreate method I restore it.
public class MainActivity extends AppCompatActivity {
private final String CURRENT_STEP_TAG = "current_step";
private int currentStep;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pedido);
if(savedInstanceState == null) {
loadFirstFragment();
} else {
currentStep = savedInstanceState.getInt(CURRENT_STEP_TAG);
if(currentStep == 2){
//Do some stuff...
}
}
}
@Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
Log.d("deubg", "--------onsaveinstancestate--------");
outState.putInt(CURRENT_STEP_TAG, currentStep);
super.onSaveInstanceState(outState, outPersistentState);
}
...
}
问题是,当屏幕方向发生更改时,不会调用onSavedInstanceState,而根据Google文档,它应该会被调用.消息"-------- onsaveinstancestate --------"未显示在控制台中. 但是,屏幕旋转后,onCreate方法中的Bundle savedInstanceState不为null,但它没有int'currentStep'.
The thing is that onSavedInstanceState won't get called when screen orentation changes, and according to google documentation, it should. The message "--------onsaveinstancestate--------" doesn't show in the console. However the Bundle savedInstanceState in the method onCreate is non null after screen rotation, but it doesn't have the int 'currentStep'.
我尝试了很多事情:将AppCompatActivity更改为ActionBarActivity,将对super.onSavedInstanceState的调用移到其他位置,尝试存储其他变量;但是无论我做什么,该方法都不会执行.
I've tried many things: changed AppCompatActivity for ActionBarActivity, moved the call for super.onSavedInstanceState to different locations, tried to store other variables; but no matter what I do the method doesn't execute.
我的清单中也没有android:configChanges.
Also I DO NOT have android:configChanges in my manifest.
我的应用程序正在针对sdk 22版buildToolsVersion 22.0.1进行编译
My application is being compiled against sdk version 22, buildToolsVersion 22.0.1
推荐答案
除非您的应用在Android的Lollipop(API21)版本或更高版本上正在运行,否则您的
Unless your app is running on Lollipop (API21) version of Android or newer, your
public void onSaveInstanceState (Bundle outState, PersistableBundle outPersistentState);
将不被调用,因为该平台在21之前的平台上根本不存在.要支持API 21之前的设备,您必须(而不是上面的)替代以下方法:>
will NOT be called as it simply does not exist on earlier versions of the platform than 21. To support pre API 21 devices you must, instead of the above, override the following method:
public void onSaveInstanceState (Bundle outState);
这同样适用于API 21+,因此,您当然不必重写这两种方法(除非您知道需要处理新提供的PersistableBundle
).
This will work on API 21+ as well, so you do not need to override both methods, of course (unless you know you need to deal with PersistableBundle
the new one offers).
参见文档.
这篇关于屏幕旋转后未调用onSaveInstanceState的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!