安卓的onSaveInstanceState没有从活动称为 [英] Android: onSaveInstanceState not being called from activity

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

问题描述

我有活性的它调用活动B.在活动B,当我点击一个按钮,完成()被调用,从而调用的onDestroy活动的B(),并返回到活动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(束捆)将被调用,在那里我这样做。

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);

    }

和下一次活动B从活动A,

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
}
}

但是,调用的onDestroy在b活动之前,该的onSaveInstanceState方法不会被调用。 有这方面的帮助将不胜AP preciated。

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 iff:

就被打死了OS 。 当这种情况发生:

it was killed by the OS. "Such situation happen when:

  • 的设备变化的方向(你的活动被破坏并重新创建)
  • 有一个在你面前另一个活动,并在某些时候OS杀死,为了您的活动来释放内存(例如)。下一次,当你开始你的活动onRestoreInstanceState()将被称为。

所以,如果你在你的活动,您打后退按钮在设备上,你的活动结束()版,并在下一次启动您的应用程序被再次启动(这听起来像重创建的,是不是?),但这次的不保存状态,因为你有意退出它,当你点击后退按钮。

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?) but this time without saved state because you intentionally exited it when you hit Back button.

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

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