Android實現(xiàn)關(guān)機后數(shù)據(jù)不會丟失問題
要實現(xiàn)關(guān)機后數(shù)據(jù)也不會丟失,需要使用到 AndroidViewModel,SaveStateHandle 和 SharePreferences 要達到的目的就是將數(shù)據(jù)保存成這個亞子
就不會出現(xiàn)app在異常閃退或者關(guān)機后數(shù)據(jù)的丟失了注意在使用SaveStateHandle和binding的時候需要在gradle里面設(shè)置一波
數(shù)據(jù)類
package com.example.applicationtest04;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.SavedStateHandle;
public class MyVIewModel extends AndroidViewModel {
SavedStateHandle handle; //聲明savedstatehandle 類型
String shpName = getApplication().getResources().getString(R.string.shp_name);
String key = getApplication().getResources().getString(R.string.key);
public MyVIewModel(@NonNull Application application, SavedStateHandle handle) {
super(application);
this.handle = handle;
if(!handle.contains(key)){
load();
}
}
public LiveData<Integer> getNumber(){
return handle.getLiveData(key);
}
public void load(){
SharedPreferences shp = getApplication().getSharedPreferences(shpName, Context.MODE_PRIVATE);
int x = shp.getInt(key,0);
handle.set(key,x);
}
public void save(){
SharedPreferences shp = getApplication().getSharedPreferences(shpName,Context.MODE_PRIVATE);
SharedPreferences.Editor editor = shp.edit();
editor.putInt(key,getNumber().getValue());
editor.apply();
}
public void add(int x){
handle.set(key,getNumber().getValue()+x);
}
}
//這段代碼里面有幾個重要的點就是在使用handle的時候要注意使用的數(shù)據(jù)是liveData
Mainactive類
package com.example.applicationtest04;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.SavedStateVMFactory;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelProviders;
import android.os.Bundle;
import com.example.applicationtest04.databinding.ActivityMainBinding;
public class MainActivity extends AppCompatActivity {
MyVIewModel myVIewModel;
ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this,R.layout.activity_main);
this.myVIewModel = ViewModelProviders.of(this,new SavedStateVMFactory(this)).get(MyVIewModel.class);
binding.setData(myVIewModel);
binding.setLifecycleOwner(this);
}
@Override
protected void onPause() {
super.onPause();
myVIewModel.save();
}
}
//這段代碼的重點就是使用onPause這個聲明周期的函數(shù)來調(diào)用save()函數(shù)
布局xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="Data"
type="com.example.applicationtest04.MyVIewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{String.valueOf(Data.getNumber())}"
android:textColor="@color/colorPrimaryDark"
android:textSize="36sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.324" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="@string/buttonPlus"
android:onClick="@{()->Data.add(1)}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.182"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.499" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="@string/buttonSub"
android:onClick="@{()->Data.add(-1)}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.804"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.499" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
測試效果先加到12
再重啟
重啟之后重新打開app
值還是沒有變化測試成功
總結(jié)
以上所述是小編給大家介紹的Android實現(xiàn)關(guān)機后數(shù)據(jù)不會丟失問題,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對我們網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
上一篇:Android集成騰訊X5實現(xiàn)文檔瀏覽功能
欄 目:Android
下一篇:Android開發(fā)之基于RecycleView實現(xiàn)的頭部懸浮控件
本文標題:Android實現(xiàn)關(guān)機后數(shù)據(jù)不會丟失問題
本文地址:http://www.jygsgssxh.com/a1/Android/9136.html
您可能感興趣的文章
- 01-10Android自定義View之繪制圓形頭像功能
- 01-10Android實現(xiàn)雙擊返回鍵退出應(yīng)用實現(xiàn)方法詳解
- 01-10android實現(xiàn)記住用戶名和密碼以及自動登錄
- 01-10android實現(xiàn)簡單計算器功能
- 01-10Android 友盟第三方登錄與分享的實現(xiàn)代碼
- 01-10C++自定義API函數(shù)實現(xiàn)大數(shù)相乘算法
- 01-10如何給Flutter界面切換實現(xiàn)點特效
- 01-10android實現(xiàn)指紋識別功能
- 01-10Emoji表情在Android JNI中的兼容性問題詳解
- 01-10Android實現(xiàn)圓形漸變加載進度條


閱讀排行
本欄相關(guān)
- 01-10Android自定義View之繪制圓形頭像功能
- 01-10Android實現(xiàn)雙擊返回鍵退出應(yīng)用實現(xiàn)方
- 01-10android實現(xiàn)簡單計算器功能
- 01-10android實現(xiàn)記住用戶名和密碼以及自動
- 01-10C++自定義API函數(shù)實現(xiàn)大數(shù)相乘算法
- 01-10Android 友盟第三方登錄與分享的實現(xiàn)代
- 01-10android實現(xiàn)指紋識別功能
- 01-10如何給Flutter界面切換實現(xiàn)點特效
- 01-10Android實現(xiàn)圓形漸變加載進度條
- 01-10Emoji表情在Android JNI中的兼容性問題詳
隨機閱讀
- 01-10delphi制作wav文件的方法
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10C#中split用法實例總結(jié)
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 04-02jquery與jsp,用jquery
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置


