Activity意外杀死保存数据

Activity意外杀死保存数据
例如
1.横竖屏切换
2.当Activity处于stop状态,可能被系统杀死

package com.luohao.hightpoint.activity;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;


public class DetailActivity extends Activity {



//如果由于某种原因当前Activity被系统意外杀死后重新创建时,savedInstanceState有值
     */

 @Overrideprotected void onCreate(Bundle savedInstanceState) {

 super.onCreate(savedInstanceState);

 setContentView(R.layout.activity_detail);



 }
    /**
 * 保存临时数据
 */@Overrideprotected void onSaveInstanceState(Bundle outState) {

    super.onSaveInstanceState(outState);


    outState.putString("key", "临时数据2222");


    System.out.println("outState = " + outState);

    }

    /**
 * 临时数据的还原一般在这
 * 如果由于某种原因当前Activity被系统意外杀死后重新创建时,savedInstanceState有值
 * */@Overrideprotected void onRestoreInstanceState(Bundle savedInstanceState) {

    super.onRestoreInstanceState(savedInstanceState);

    System.out.println("DetailActivity.onRestoreInstanceState");

    String string = savedInstanceState.getString("key");


}

你可能感兴趣的:(Android开发)