Android學習小結(jié)之Activity保存和恢復狀態(tài)
Android中啟動一個Activity如果點擊Home鍵該Activity是不會被銷毀的,但是當進行某些操作時某些數(shù)據(jù)就會丟失,如下:
Java class:
package com.king.activitytest2; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private EditText editText; private Button buttonSet,buttonRead; //定義一個常量 double pai; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //獲取控件 editText=(EditText)findViewById(R.id.edit1); buttonSet=(Button) findViewById(R.id.btn_Set); buttonRead=(Button) findViewById(R.id.btn_Read); //設(shè)置監(jiān)聽事件 buttonSet.setOnClickListener(this); buttonRead.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.btn_Set: editText.setText("圓周率為:"); pai=3.141592654; break; case R.id.btn_Read: String str=editText.getText().toString()+pai; Toast.makeText(MainActivity.this,str,Toast.LENGTH_SHORT).show(); break; } } }
xml布局文件:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.king.activitytest2.MainActivity"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/edit1"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="設(shè)置" android:id="@+id/btn_Set" android:layout_marginTop="46dp" android:layout_below="@+id/edit1" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginLeft="35dp" android:layout_marginStart="35dp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="讀取" android:id="@+id/btn_Read" android:layout_alignTop="@+id/btn_Set" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:layout_marginRight="46dp" android:layout_marginEnd="46dp" /> </RelativeLayout>
這里在程序中定義了一個double類型的變量,當我們點擊讀取按鈕時會將該變量的值加在后面并顯示,打開程序然后進行操作,一切正常:
我們點擊設(shè)置之后將屏幕橫過來,咦,pai跑哪去了?
其實這里當我們進行橫豎屏切換時,迫于系統(tǒng)機制,該Activity已經(jīng)被銷毀了。但是為什么這個頁面還存在呢,這是因為這個銷毀并不是用戶去主動退出,所以Android系統(tǒng)實現(xiàn)了這些狀態(tài)的保存功能,但是一些數(shù)據(jù)達不到被保留的要求,并沒有被保留,就像程序中可憐的pai就被拋棄了!
那我們需要在像這種橫豎屏切換過程中保留數(shù)據(jù)該如何做呢,這里我們就需要重寫onSaveInstanceState()方法:
@Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); //將需要保存的數(shù)據(jù)放入Bundle中 outState.putDouble("pai",pai); }
保存解決了,那我們?nèi)绾稳〕瞿兀亢芎唵?,只需要在onCreate()方法中判斷其參數(shù)是否為null,不為null便將其取出。
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //將pai保存 if(savedInstanceState!=null) pai=savedInstanceState.getDouble("pai"); }
現(xiàn)在我們可以看到屏幕切換之后pai也沒有被銷毀。
以上所述是小編給大家介紹的Activity保存和恢復狀態(tài),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- android中Activity詳解(生命周期、以各種方式啟動Activity、狀態(tài)保存,完全退出等)
- Android保存Activity狀態(tài)的方法
- Android 保存Fragment 切換狀態(tài)實例代碼
- Android中用onSaveInstanceState保存Fragment狀態(tài)的方法
- 實例探究Android開發(fā)中Fragment狀態(tài)的保存與恢復方法
- android初學者必須掌握的Activity狀態(tài)的四大知識點(必讀)
- Android編程實現(xiàn)隱藏狀態(tài)欄及測試Activity是否活動的方法
- Android中檢查、監(jiān)聽電量和充電狀態(tài)的方法
- Android中監(jiān)聽軟鍵盤顯示狀態(tài)實現(xiàn)代碼
- Android編程實現(xiàn)狀態(tài)保存的方法分析
相關(guān)文章
詳解Retrofit 動態(tài)參數(shù)(非固定參數(shù)、非必須參數(shù))(Get、Post請求)
這篇文章主要介紹了詳解Retrofit 動態(tài)參數(shù)(非固定參數(shù)、非必須參數(shù))(Get、Post請求),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04

Android自定義控件之繼承ViewGroup創(chuàng)建新容器