最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android實(shí)現(xiàn)關(guān)機(jī)后數(shù)據(jù)不會丟失問題

 更新時間:2019年10月28日 15:19:06   作者:毛少雄  
這篇文章主要介紹了Android實(shí)現(xiàn)關(guān)機(jī)后數(shù)據(jù)不會丟失問題,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

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

在這里插入圖片描述

就不會出現(xiàn)app在異常閃退或者關(guān)機(jī)后數(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);
 }
}
//這段代碼里面有幾個重要的點(diǎn)就是在使用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();
 }
}
//這段代碼的重點(diǎn)就是使用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實(shí)現(xiàn)關(guān)機(jī)后數(shù)據(jù)不會丟失問題,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!

相關(guān)文章

  • Android開發(fā)之使用通知欄顯示提醒信息的方法

    Android開發(fā)之使用通知欄顯示提醒信息的方法

    這篇文章主要介紹了Android開發(fā)之使用通知欄顯示提醒信息的方法,涉及Notification的使用及通知欄信息設(shè)置技巧,需要的朋友可以參考下
    2016-01-01
  • Android6.0獲取GPS定位和獲取位置權(quán)限和位置信息的方法

    Android6.0獲取GPS定位和獲取位置權(quán)限和位置信息的方法

    今天小編就為大家分享一篇Android6.0獲取GPS定位和獲取位置權(quán)限和位置信息的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • android getActivity.findViewById獲取ListView 返回NULL的方法

    android getActivity.findViewById獲取ListView 返回NULL的方法

    下面小編就為大家?guī)硪黄猘ndroid getActivity.findViewById獲取ListView 返回NULL的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-11-11
  • Android開發(fā)中setContentView和inflate的區(qū)別分析

    Android開發(fā)中setContentView和inflate的區(qū)別分析

    這篇文章主要介紹了Android開發(fā)中setContentView和inflate的區(qū)別,較為詳細(xì)的分析了setContentView和inflate的功能、用法及二者的區(qū)別,需要的朋友可以參考下
    2016-07-07
  • Android實(shí)現(xiàn)翻頁特效

    Android實(shí)現(xiàn)翻頁特效

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)翻頁特效,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • Android 滑動監(jiān)聽的實(shí)例詳解

    Android 滑動監(jiān)聽的實(shí)例詳解

    這篇文章主要介紹了Android 滑動監(jiān)聽的實(shí)例詳解的相關(guān)資料,希望通過本能幫助到大家,需要的朋友可以參考下
    2017-09-09
  • SharedPreference引發(fā)ANR原理詳解

    SharedPreference引發(fā)ANR原理詳解

    這篇文章主要為大家介紹了SharedPreference引發(fā)ANR原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02
  • Android RecyclerView實(shí)現(xiàn)滑動刪除

    Android RecyclerView實(shí)現(xiàn)滑動刪除

    這篇文章主要為大家詳細(xì)介紹了Android RecyclerView實(shí)現(xiàn)滑動刪除,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-07-07
  • Android編程實(shí)現(xiàn)打勾顯示輸入密碼功能

    Android編程實(shí)現(xiàn)打勾顯示輸入密碼功能

    這篇文章主要介紹了Android編程實(shí)現(xiàn)打勾顯示輸入密碼功能,涉及Android控件布局及屬性相關(guān)操作技巧,需要的朋友可以參考下
    2017-02-02
  • Flutter開發(fā)之Widget自定義總結(jié)

    Flutter開發(fā)之Widget自定義總結(jié)

    這篇文章主要給大家介紹了關(guān)于Flutter開發(fā)中Widget自定義的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Flutter具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04

最新評論

龙海市| 桐城市| 通榆县| 宜兰市| 平乡县| 琼中| 大同市| 巴林左旗| 泰宁县| 介休市| 时尚| 临泽县| 霞浦县| 伽师县| 布尔津县| 长治县| 大余县| 盘锦市| 泸州市| 卢湾区| 吉首市| 宜川县| 泰安市| 和顺县| 红安县| 乌兰察布市| 彭州市| 万山特区| 井冈山市| 余庆县| 花莲市| 花莲市| 津南区| 黑山县| 溧阳市| 云安县| 隆子县| 澄江县| 封丘县| 松潘县| 兴山县|