方向更改时保存数据

时间:2015-05-23 18:40:50

标签: android android-savedstate

我在onCreate()方法中添加了一个条件来检查是否存在某些先前的数据。但它没有用。 请指导我。 而且我不想为this.d使用onRestoreSavedState()方法

public class ActivityOne extends Activity {

在重新配置之间保存状态时使用这些作为键

private static final String RESTART_KEY = "restart";
private static final String RESUME_KEY = "resume";
private static final String START_KEY = "start";
private static final String CREATE_KEY = "create";

LogCat文档的字符串

private final static String TAG = "Lab-ActivityOne";
private int mCreate=0, mRestart=0, mStart=0, mResume=0;

您需要增加这些变量'他们的价值观 相应的生命周期方法被调用。

TODO:为每个TextView创建变量

private TextView mTvCreate, mTvRestart, mTvStart, mTvResume;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_one);

TODO:将相应的TextViews分配给TextView变量

    mTvCreate=(TextView)findViewById(R.id.create);
    mTvRestart=(TextView)findViewById(R.id.restart);
    mTvResume=(TextView)findViewById(R.id.resume);
    mTvStart=(TextView)findViewById(R.id.start);


    Button launchActivityTwoButton = (Button) findViewById(R.id.bLaunchActivityTwo);
    launchActivityTwoButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

           Intent intent=null;
            intent = new Intent(v.getContext(),ActivityTwo.class);
        startActivity(intent);

        }
    });
    if(savedInstanceState!=null){
        mCreate=savedInstanceState.getInt(String.valueOf(mCreate));
        mRestart=savedInstanceState.getInt(String.valueOf(mRestart));
        mResume=savedInstanceState.getInt(String.valueOf(mResume));
        mStart=savedInstanceState.getInt(String.valueOf(mStart));
    }


    // Emit LogCat message
    Log.i(TAG, "Entered the onCreate() method");
        mCreate++;
        displayCounts();
}

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {

使用一组键值对保存状态信息 4行代码,每个计数变量一行         super.onSaveInstanceState(savedInstanceState);

    savedInstanceState.putInt(String.valueOf(mCreate),mCreate);
    savedInstanceState.putInt(String.valueOf(mRestart),mRestart);
    savedInstanceState.putInt(String.valueOf(mResume),mResume);
    savedInstanceState.putInt(String.valueOf(mStart),mStart);

}

更新显示的计数器 此方法需要计数器和TextView变量使用上面指定的名称     public void displayCounts(){

    mTvCreate.setText("onCreate() calls: " + mCreate);
    mTvStart.setText("onStart() calls: " + mStart);
    mTvResume.setText("onResume() calls: " + mResume);
    mTvRestart.setText("onRestart() calls: " + mRestart);

}

}

1 个答案:

答案 0 :(得分:1)

看起来问题是当您尝试捕获已保存的值时,尝试使用尚未设置的值。

您需要做的就是使用已定义的字符串常量。

因此,在onCreate()中,您将拥有:

if(savedInstanceState!=null){
    mCreate=savedInstanceState.getInt(CREATE_KEY);
    mRestart=savedInstanceState.getInt(RESTART_KEY);
    mResume=savedInstanceState.getInt(RESUME_KEY);
    mStart=savedInstanceState.getInt(START_KEY);
}

然后,在onSaveInstanceState()中,您将拥有:

savedInstanceState.putInt(CREATE_KEY,mCreate);
savedInstanceState.putInt(RESTART_KEY,mRestart);
savedInstanceState.putInt(RESUME_KEY,mResume);
savedInstanceState.putInt(START_KEY,mStart);