如何留住活动在android系统状态 [英] how to retain the state of activity in android

查看:165
本文介绍了如何留住活动在android系统状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何在保持Android的一个活动的状态?我在布局和布局,土地两个布局纵向和横向。我在我显示进度对话框中的加载时间从服务的价值。如果加载的用户的时候旋转设备为横向装载也。我该如何避免这种情况?用户键入的WebView内容也刷新。我该如何避免这种情况,有谁能够提供一个例子?

How do I retain the state of an activity in android? I have two layouts for portrait and landscape in layout and layout-land. I am loading the value from service at the time I am showing progress dialog. If loaded user rotates the device to landscape at the time also loading. How do I avoid that? user typed content in webview that also refreshed. How do I avoid that, can anybody provide an example?

感谢

推荐答案

您可以使用 onRetainNonConfigurationChange()回调存储任意数据。这就是所谓的之前您的应用程序要重新创建。

You can use the onRetainNonConfigurationChange() callback to store arbitrary data. It is called just before your application is about to be recreated.

然后,在的onCreate()只是检查,如果一些数据被调用 getLastNonConfigurationInstance()即抛开回报对象抛开或者为null。

Then, in onCreate() just check if some data were put aside by calling getLastNonConfigurationInstance() that returns the Object you put aside or null.

请参阅在Android开发者这篇文章

下面是从上面的链接借用一个样本:

Here's a sample borrowed from the link above:

@Override
public Object onRetainNonConfigurationInstance() {
    //this is called by the framework when needed
    //Just return what you want to save here.

    return MyBigObjectThatContainsEverythingIWantToSave;
}

AUTOMAGIC previously保存状态的恢复:

Automagic restore of previously saved state:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final MyDataObject MyBigObjectThatContainsEverythingIWantToSave = (MyDataObject) getLastNonConfigurationInstance();
    if (MyBigObjectThatContainsEverythingIWantToSave == null) {
        //No saved state
        MyBigObjectThatContainsEverythingIWantToSave = loadMyData();
    } else {
        //State was restored, no need to download again.
    }
    ...
}

这篇关于如何留住活动在android系统状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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