In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail how to use Android to achieve the problem that data will not be lost after shutdown. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
In order to ensure that the data will not be lost after shutdown, you need to use AndroidViewModel,SaveStateHandle and SharePreferences to achieve the goal of saving the data as this sub-sub.
There will be no data loss of app after abnormal flashback or shutdown. Note that when using SaveStateHandle and binding, you need to set a wave in gradle.
Data class
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; / / declares savedstatehandle type 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 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);}} / / there are several important points in this code, that is, the data you should pay attention to when using handle is liveData.
Mainactive class
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 ();}} / / the focus of this code is to use the function of onPause to declare the period to call the save () function.
Layout xml
This is the end of the article on "how to use Android to achieve data loss after shutdown". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.