`

onCreate中的savedInstanceState有何具体作用

阅读更多

在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 demo

    通过以上讲解,我们了解了`savedInstanceState`在Android开发中的重要作用,以及如何正确地使用它来保存和恢复应用状态。掌握这些知识,能帮助我们构建更健壮、用户体验更好的应用。在实际项目中,应根据具体需求...

    PinnedSectionListview

    protected void onCreate Bundle savedInstanceState { super onCreate savedInstanceState ; setContentView R layout activity main ; if savedInstanceState null { isFastScroll savedInstanceState ...

    Android代码-简单实用的页面多状态布局(content,loading,empty,error)

    LoadingLayout 简单实用的页面多状态布局(content,...protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // ... vLoad

    Android代码-CatLoadingView

    Android CatLoadingView This project idea is from Link. ...@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);

    Android代码-一个简洁的基于 RxJava2 的事件总线库

    protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 注册 String 类型事件 RxBus.getDefault().subscribe(this, new ...

    Android 柱状图

    protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initData(); LinearLayout layout = (LinearLayout) findViewById...

    Android代码-RxBus2

    protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); RxBus.get().register(this); } public void test1005(View view){ ...

    Android代码-YLListView仿IOS弹簧效果的ListView

    protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (YLListView) findViewById(R.id.listView); // 不添加也有...

    yinyue.zip

    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) { ...

    android时间控件

    public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initializeViews(); final Calendar c = Calendar.getInstance()...

    一个封装WebView的库

    环境搭建新建一个activity继承WebActivity @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setWebChromeClient(new ChromeClient());//设置...

    Android代码-安卓路由解决方案

    HippoRouter Android端URL路由解决方案... protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_example); initDataFromIntent(); } p

    android listview 上下滑动刷新

    public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.pull_to_refresh); ((PullToRefreshListView) getListView()).setOnRefreshListener(new ...

    Android代码-Immersive

    protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Immersive.setContentView(this, R.layout.activity_main, R.color.blue, R.color.green, false, false); } } ...

    Android ListView简单应用

    protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ArrayAdapter<String> arrayAdapter=new ArrayAdapter( MainActivity....

    Android异步查询数据库工具类LoaderCursor

    protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LoaderCursorUtil cursorUtil = new LoaderCursorUtil(this); cursorUtil.getCurosr(new DBCursorListener() { ...

    Android代码-TouchRegion

    TouchRegion Scene View整体尺寸小,点击区域小,需要点击很多次才能选中; 通过设置View的padding属性增大... ... ... protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); set

    butterknife,《注释》杂志.zip

    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 = ...

Global site tag (gtag.js) - Google Analytics