Android:没有从活动中调用 onSaveInstanceState [英] Android: onSaveInstanceState not being called from activity

查看:17
本文介绍了Android:没有从活动中调用 onSaveInstanceState的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动 A,它调用活动 B.在活动 B 中,当我点击一个按钮时,finish() 被调用,它依次调用活动 B 的 onDestroy() 并返回活动 A.

I have an Activity A which calls Activity B. In Activity B, when i click on a button, finish() is called, which in turn calls onDestroy() of Activity B and returns to activity A.

根据 android 文档,在调用 onDestroy 之前,将调用 onSaveInstanceState(Bundle bundle),我执行以下操作.

According to android documentation, Before onDestroy is called, onSaveInstanceState(Bundle bundle) will be called, where i do the following.

@Override
    public void onSaveInstanceState(Bundle outState) {

        super.onSaveInstanceState(outState);
        System.out.println("Saving webview state");
        Log.d(TAG, "In onsave");
        wv.saveState(outState);

    }

下次从活动A开始活动B时,

and the next time Activity B is started from Activity A,

在 oncreate() 中,我执行以下操作:

in the oncreate(), i do the following:

onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);

if(savedInstanceState != null){
//restore webview
}else {
// code
}
}

但是,在Activity B中调用onDestroy之前,从来没有调用过onSaveInstanceState方法.对此的任何帮助将不胜感激.

However, before calling onDestroy in Activity B, The onSaveInstanceState method is never called. any help on this will be greatly appreciated.

如果这是不可能的.请让我知道是否有办法存储 webview 状态

if this is not possible. Please let me know if there is a way to store webview state

推荐答案

请注意 onRestoreInstanceState() 在重新创建活动时被调用,但前提是:

Please notice that onRestoreInstanceState() is called when activity is recreated but only if:

它被操作系统杀死.这种情况发生在:

  • 设备的方向发生变化(您的活动被销毁并重新创建)
  • 在您的前面还有另一个活动,在某些时候操作系统会终止您的活动以释放内存(例如).下次启动活动时 onRestoreInstanceState() 将被调用."
  • orientation of the device changes (your activity is destroyed and recreated)
  • there is another activity in front of yours and at some point the OS kills your activity in order to free memory (for example). Next time when you start your activity onRestoreInstanceState() will be called."

因此,如果您在您的活动中并点击设备上的后退按钮,您的活动将被finish()并在您下次启动您的应用时启动再次(听起来像是重新创建,不是吗?)但这次没有保存状态,因为您在点击返回"按钮时有意退出了它.

So if you are in your activity and you hit Back button on the device, your activity is finish()ed and next time you start your app it is started again (it sounds like re-created, isn't it?) but this time without saved state because you intentionally exited it when you hit Back button.

这篇关于Android:没有从活动中调用 onSaveInstanceState的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆