onCreate的savedInstanceState为何一直是null?

中才德创 中才德创 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
onCreate
onSaveInstanceState
onRestoreInstanceState
各是什么时机运行?

package com.androidbook;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class HelloActivity extends Activity {
private static final String TAG = "MyNewLog";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*
* If an instance of this activity had previously stopped, we can
* get the original text it started with.
* */
if (null != savedInstanceState)
{
int IntTest = savedInstanceState.getInt("IntTest");
String StrTest = savedInstanceState.getString("StrTest");
Log.e(TAG, "onCreate get the savedInstanceState+IntTest="+IntTest+"+StrTest="+StrTest);
}
setContentView(R.layout.main);
Log.e(TAG, "onCreate");
}

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
/*
* Save away the original text, so we still have it if the activity
* needs to be killed while paused.
* */
savedInstanceState.putInt("IntTest", 0);
savedInstanceState.putString("StrTest", "savedInstanceState test");
super.onSaveInstanceState(savedInstanceState);
Log.e(TAG, "onSaveInstanceState");
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
int IntTest = savedInstanceState.getInt("IntTest");
String StrTest = savedInstanceState.getString("StrTest");
Log.e(TAG, "onRestoreInstanceState+IntTest="+IntTest+"+StrTest="+StrTest);
}
}
...全文
评论 5
收藏 已收藏
分享
返回顶部
中才德创
Android 80,488 成员 91,371 内容