安卓保护现场实例

在安卓的开发过程中,经常需要对之前的数据进行保存。这个时候就需要进行保护现场的操作,该操作主要是重写了onSaveInstanceState和onRestoreInstanceState两个函数。

onSaveInstanceState,保存数据

活动生命周期的执行顺序:
onPause-onSaveInstanceState-onStop-onDestroy

onRestoreInstanceState,恢复数据

活动生命周期的执行顺序:
onStart-onRestoreInstanceState-onResume

下面这个例子就以手机横竖屏为例,保护了背景颜色这一数据。

package com.example.hjw.androidday0602;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

import java.util.Random;

public class MainActivity extends AppCompatActivity {
    private LinearLayout linearLayout;
    private Button button;
    String r,g,b,c;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        linearLayout = (LinearLayout)findViewById(R.id.l1);
        button = (Button)findViewById(R.id.b1);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Random random = new Random();
                r = Integer.toHexString(random.nextInt(256)).toUpperCase();
                g = Integer.toHexString(random.nextInt(256)).toUpperCase();
                b = Integer.toHexString(random.nextInt(256)).toUpperCase();

                r = r.length()==1 ? "0" + r : r ;
                g = g.length()==1 ? "0" + g : g ;
                b = b.length()==1 ? "0" + b : b ;
                c="#"+r+b+g;
                Toast.makeText(MainActivity.this,c,Toast.LENGTH_SHORT).show();
                linearLayout.setBackgroundColor(Color.parseColor(c));
            }
        });

    }
    // 保存数据
    // 参数为Bundle类型,用于存储数据
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        // TODO Auto-generated method stub
        super.onSaveInstanceState(outState);
        if (c!=null){
            outState.putString("color", c);
        }
    }
    // 恢复数据
    // 参数为Bundle类型,用于读取数据
    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onRestoreInstanceState(savedInstanceState);

        c = savedInstanceState.getString("color");
        linearLayout.setBackgroundColor(Color.parseColor(c));
    }

    // 重新将值恢复
    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        Toast.makeText(MainActivity.this,c,Toast.LENGTH_SHORT).show();


    }
}

前台代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/l1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.hjw.androidday0602.MainActivity">


    <Button
        android:id="@+id/b1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="跳转"/>
</LinearLayout>

 运行效果:

posted @ 2016-05-18 08:32  九天鲲鹏  阅读(258)  评论(0编辑  收藏  举报