在Android上保存活动状态 [英] Saving Activity state on Android

查看:274
本文介绍了在Android上保存活动状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在玩弄Android的SDK,我上保存应用程序的状态有点不清楚。因此,鉴于这种少量的重新加工的你好,Android的例子:

I've been playing around with the Android SDK, and I am a little unclear on saving an application's state. So given this minor re-tooling of the 'Hello, Android' example:

package com.android.hello;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class HelloAndroid extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mTextView = new TextView(this);

        if (savedInstanceState == null) {
            mTextView.setText("Welcome to HelloAndroid!");
        } else {
            mTextView.setText("Welcome back.");
        }

        setContentView(mTextView);
    }

    private TextView mTextView = null;
}

我想这可能是所有的人需要做的最简单的情况,但它总是给我的第一个消息,不管我怎么导航离开应用程序。我敢肯定,这可能是一些简单的像覆盖的onPause或类似的东西,但我一直在走戳的文档中30分钟左右,并没有发现任何明显的,所以我会AP preciate任何帮助。

I thought that might be all one needed to do for the simplest case, but it always gives me the first message, no matter how I navigate away from the app. I'm sure it's probably something simple like overriding onPause or something like that, but I've been poking away in the documentation for 30 minutes or so and haven't found anything obvious, so I would appreciate any help.

推荐答案

您需要重写的onSaveInstanceState(包savedInstanceState),写要更改应用程序的状态值像这样的捆绑参数:

You need to override onSaveInstanceState(Bundle savedInstanceState) and write the application state values you want to change to the Bundle parameter like this:

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
  super.onSaveInstanceState(savedInstanceState);
  // Save UI state changes to the savedInstanceState.
  // This bundle will be passed to onCreate if the process is
  // killed and restarted.
  savedInstanceState.putBoolean("MyBoolean", true);
  savedInstanceState.putDouble("myDouble", 1.9);
  savedInstanceState.putInt("MyInt", 1);
  savedInstanceState.putString("MyString", "Welcome back to Android");
  // etc.
}

捆绑本质上是存储NVP(名称 - 值对)的地图的一种方式,它会获得通过,以的onCreate(),也 onRestoreInstanceState()在这里你会提取值是这样的:

The Bundle is essentially a way of storing a NVP ("Name-Value Pair") map, and it will get passed in to onCreate() and also onRestoreInstanceState() where you'd extract the values like this:

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
  super.onRestoreInstanceState(savedInstanceState);
  // Restore UI state from the savedInstanceState.
  // This bundle has also been passed to onCreate.
  boolean myBoolean = savedInstanceState.getBoolean("MyBoolean");
  double myDouble = savedInstanceState.getDouble("myDouble");
  int myInt = savedInstanceState.getInt("MyInt");
  String myString = savedInstanceState.getString("MyString");
}

您通常会使用此技术来存储实例值的应用程序(选择,未保存的文本,等等)。

You would usually use this technique to store instance values for your application (selections, unsaved text, etc.).

这篇关于在Android上保存活动状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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