Android實(shí)現(xiàn)關(guān)機(jī)后數(shù)據(jù)不會(huì)丟失問(wèn)題
要實(shí)現(xiàn)關(guān)機(jī)后數(shù)據(jù)也不會(huì)丟失,需要使用到 AndroidViewModel,SaveStateHandle 和 SharePreferences 要達(dá)到的目的就是將數(shù)據(jù)保存成這個(gè)亞子

就不會(huì)出現(xiàn)app在異常閃退或者關(guān)機(jī)后數(shù)據(jù)的丟失了注意在使用SaveStateHandle和binding的時(shí)候需要在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);
}
}
//這段代碼里面有幾個(gè)重要的點(diǎn)就是在使用handle的時(shí)候要注意使用的數(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();
}
}
//這段代碼的重點(diǎn)就是使用onPause這個(gè)聲明周期的函數(shù)來(lái)調(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>
測(cè)試效果先加到12

再重啟

重啟之后重新打開(kāi)app

值還是沒(méi)有變化測(cè)試成功
總結(jié)
以上所述是小編給大家介紹的Android實(shí)現(xiàn)關(guān)機(jī)后數(shù)據(jù)不會(huì)丟失問(wèn)題,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
- android實(shí)現(xiàn)短按電源鍵關(guān)機(jī)的實(shí)現(xiàn)代碼
- Android平臺(tái)預(yù)置GMS包后關(guān)機(jī)鬧鐘失效問(wèn)題及解決方法
- Android 6.0開(kāi)發(fā)實(shí)現(xiàn)關(guān)機(jī)菜單添加重啟按鈕的方法
- Android開(kāi)發(fā)實(shí)現(xiàn)長(zhǎng)按返回鍵彈出關(guān)機(jī)框功能
- Android仿蘋果關(guān)機(jī)界面實(shí)現(xiàn)代碼
- Android 修改系統(tǒng)關(guān)機(jī)動(dòng)畫(huà)的實(shí)現(xiàn)
- Android下的CMD命令之關(guān)機(jī)重啟及重啟recovery
- Android實(shí)現(xiàn)關(guān)機(jī)與重啟的幾種方式(推薦)
- Android系統(tǒng)關(guān)機(jī)的全流程解析
- Android 實(shí)現(xiàn)關(guān)機(jī)的多種方式
相關(guān)文章
Android用PopupWindow實(shí)現(xiàn)自定義overflow
這篇文章主要介紹了Android用PopupWindow實(shí)現(xiàn)自定義overflow的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
Android-Jetpack-Navigation組件使用示例
這篇文章主要介紹了Android-Jetpack-Navigation組件使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
詳解Android Activity中的幾種監(jiān)聽(tīng)器和實(shí)現(xiàn)方式
這篇文章主要介紹了Activity中的幾種監(jiān)聽(tīng)器和實(shí)現(xiàn)方式的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下2021-04-04
Android UI實(shí)現(xiàn)底部切換標(biāo)簽fragment
這篇文章主要為大家詳細(xì)介紹了Android UI實(shí)現(xiàn)底部切換標(biāo)簽的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
Android利用ViewPager實(shí)現(xiàn)可滑動(dòng)放大縮小畫(huà)廊效果
這篇文章主要介紹了Android利用ViewPager實(shí)現(xiàn)可滑動(dòng)放大縮小畫(huà)廊效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08
詳解Android平臺(tái)上讀寫NFC標(biāo)簽
NFC,即Near Field Communication,近距離無(wú)線通訊技術(shù),是一種短距離的(通常<=4cm或更短)高頻(13.56M Hz)無(wú)線通信技術(shù),可以讓消費(fèi)者簡(jiǎn)單直觀地交換信息、訪問(wèn)內(nèi)容與服務(wù)。2017-01-01
android不同activity之間共享數(shù)據(jù)解決方法
最近做局域網(wǎng)socket連接問(wèn)題,要在多個(gè)activity之間公用一個(gè)socket連接,就在網(wǎng)上搜了下資料,感覺(jué)還是application方法好用,帖出來(lái)需要的朋友可以參考下2012-11-11

