How to realize the function of multi-page data sharing by Android saving data through ViewModel
Editor to share with you how Android saves data through ViewModel to achieve multi-page data sharing function, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Data sharing through ViewModel conforms to Android's MVC design pattern and makes the data independent.
Realized Demo
1. The main page changes the value of the number through SeekBar.
2. Click to enter the second interface, but the data is still shared.
3. Add two numbers at random and switch again
4. Find that the data is still shared.
Here are the specific implementation steps:
1. Set up two Fragment (using Binding and Navigation)
Add Binding and Navigation at one point, or you can't do it.
2. Set up a class that inherits from ViewModel
3. Data sharing can be realized by using the class inherited from ViewModel in the code of two Fragment respectively.
Here is the specific code:
1. Classes that inherit from ViewModel
Package com.example.naviation01;import androidx.lifecycle.MutableLiveData;import androidx.lifecycle.ViewModel;public class MyViewMode extends ViewModel {private MutableLiveData number; public MutableLiveData getNumber () {if (this.number = = null) {this.number = new MutableLiveData (); this.number.setValue (0);} return this.number;} public void add (int x) {this.number.setValue (this.number.getValue () + x); if (this.number.getValue () < 0) {this.number.setValue (0);}
2. Fragment homepage
Package com.example.naviation01;import android.os.Bundle;import androidx.databinding.DataBindingUtil;import androidx.fragment.app.Fragment;import androidx.fragment.app.FragmentController;import androidx.lifecycle.ViewModel;import androidx.lifecycle.ViewModelProvider;import androidx.lifecycle.ViewModelProviders;import androidx.navigation.NavController;import androidx.navigation.Navigation;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.SeekBar;import com.example.naviation01.databinding.FragmentHomeBinding;/** * A simple {@ link Fragment} subclass. * / public class HomeFragment extends Fragment {public HomeFragment () {/ / Required empty public constructor} @ Override public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {/ / Inflate the layout for this fragment final MyViewMode myViewMode; myViewMode = ViewModelProviders.of (getActivity ()) .get (MyViewMode.class); FragmentHomeBinding binding; binding = DataBindingUtil.inflate (inflater,R.layout.fragment_home,container,false); binding.setData (myViewMode); binding.setLifecycleOwner (getActivity ()); binding.seekBar.setProgress (myViewMode.getNumber (). GetValue ()) Binding.seekBar.setOnSeekBarChangeListener (new SeekBar.OnSeekBarChangeListener () {@ Override public void onProgressChanged (SeekBar seekBar, int progress, boolean fromUser) {myViewMode.getNumber () .setValue (progress);} @ Override public void onStartTrackingTouch (SeekBar seekBar) {} @ Override public void onStopTrackingTouch (SeekBar seekBar) {}}); binding.button.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View v) {NavController controller = Navigation.findNavController (v); controller.navigate (R.id.action_homeFragment_to_detailFragment) }}); return binding.getRoot (); / / return inflater.inflate (R.layout.fragment_home, container, false);}}
Xml
3. Fragment secondary page
Package com.example.naviation01;import android.os.Bundle;import androidx.databinding.DataBindingUtil;import androidx.fragment.app.Fragment;import androidx.lifecycle.ViewModelProviders;import androidx.navigation.NavController;import androidx.navigation.Navigation;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import com.example.naviation01.databinding.FragmentDetailBinding;/** * A simple {@ link Fragment} subclass. * / public class DetailFragment extends Fragment {public DetailFragment () {/ / Required empty public constructor} @ Override public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {/ / Inflate the layout for this fragment MyViewMode myViewMode; myViewMode = ViewModelProviders.of (getActivity ()) .get (MyViewMode.class); FragmentDetailBinding binding; binding = DataBindingUtil.inflate (inflater,R.layout.fragment_detail,container,false); binding.setDate (myViewMode); binding.setLifecycleOwner (getActivity ()) Binding.button4.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View v) {NavController controller = Navigation.findNavController (v); controller.navigate (R.id.action_detailFragment_to_homeFragment);}}); return binding.getRoot (); / / return inflater.inflate (R.layout.fragment_detail, container, false);}}
Xml
4 、 xml Main_Activity
These are all the contents of the article "how Android saves data through ViewModel to achieve multi-page data sharing". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!