在activity的生命周期中,只要离开了可见阶段,或者说失去了焦点,activity就很可能被进程终止了!,被KILL掉了,,这时候,就需要有种机制,能保存当时的状态,这就是savedInstanceState的作用。
当一个Activity在PAUSE时,被kill之前,它可以调用onSaveInstanceState()来保存当前activity的状态信息(在paused状态时,要被KILLED的时候)。用来保存状态信息的Bundle会同时传给两个method,即onRestoreInstanceState() and onCreate().
示例代码如下:
package com.myandroid.test; import android.app.Activity; import android.os.Bundle; import android.util.Log; public class AndroidTest 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); } }
http://blog.csdn.net/wangjia55/article/details/8226568
相关推荐
通过以上讲解,我们了解了`savedInstanceState`在Android开发中的重要作用,以及如何正确地使用它来保存和恢复应用状态。掌握这些知识,能帮助我们构建更健壮、用户体验更好的应用。在实际项目中,应根据具体需求...
protected void onCreate Bundle savedInstanceState { super onCreate savedInstanceState ; setContentView R layout activity main ; if savedInstanceState null { isFastScroll savedInstanceState ...
LoadingLayout 简单实用的页面多状态布局(content,...protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // ... vLoad
Android CatLoadingView This project idea is from Link. ...@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 注册 String 类型事件 RxBus.getDefault().subscribe(this, new ...
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initData(); LinearLayout layout = (LinearLayout) findViewById...
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); RxBus.get().register(this); } public void test1005(View view){ ...
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (YLListView) findViewById(R.id.listView); // 不添加也有...
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.login); TextView registerScreen = (TextView) findViewById(R.id.link_to_register); ...
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { ...
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initializeViews(); final Calendar c = Calendar.getInstance()...
环境搭建新建一个activity继承WebActivity @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setWebChromeClient(new ChromeClient());//设置...
HippoRouter Android端URL路由解决方案... protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_example); initDataFromIntent(); } p
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.pull_to_refresh); ((PullToRefreshListView) getListView()).setOnRefreshListener(new ...
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Immersive.setContentView(this, R.layout.activity_main, R.color.blue, R.color.green, false, false); } } ...
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ArrayAdapter<String> arrayAdapter=new ArrayAdapter( MainActivity....
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LoaderCursorUtil cursorUtil = new LoaderCursorUtil(this); cursorUtil.getCurosr(new DBCursorListener() { ...
TouchRegion Scene View整体尺寸小,点击区域小,需要点击很多次才能选中; 通过设置View的padding属性增大... ... ... protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); set
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); // 这一行会自动查找并绑定所有注解过的视图 ...
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // get handles to the LunarView from XML, and its LunarThread jqView = ...