码迷,mamicode.com
首页 > 移动开发 > 详细

Android课程---Activity中保存和恢复用户状态

时间:2016-03-22 00:40:07      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

 

 

 

onSaveInstanceState      保存  在暂停之后和保存之前调用
onRestoreInstanceState   恢复  再启动之后和显示之前调用

 

package com.example.chenshuai.excise;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

/**
 * Created by chenshuai on 2016/3/20.
 */
public class excise1 extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.excise1);
        Log.e("tag","创建");
    }

   @Override
    protected void onStart() {
        super.onStart();
        Log.e("tag","启动");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.e("tag","显示");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.e("tag","暂停");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.e("tag","停止");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.e("tag","重新启动");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.e("tag","销毁");
    }

    //保存和恢复用户状态
    int i = 0;

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        i++;

        Log.e("tag","保存用户的状态"+i);

        //以put方法存储数据,存储形式为键值对
        outState.putInt("key",i);
    }
    //恢复用户之前保存的状态
    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);


        int i = savedInstanceState.getInt("key");
        Log.e("tag","获取用户之前保存的状态"+i);
    }

 

Android课程---Activity中保存和恢复用户状态

标签:

原文地址:http://www.cnblogs.com/0927wyj/p/5304327.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!