Skip to content

Data of android UI is saved during configuration changes of device using view model of life cycle aware components. Different kind of android life cycle aware components. By using these component an app code becomes robust, testable, and maintainable.

Notifications You must be signed in to change notification settings

dinkar1708-zz/Lifecycle-Aware-Components

Repository files navigation

Android life cycle aware components

Components Architecture components are a set of Android libraries that help you structure your app in a way that is robust, testable, and maintainable

INPUT - EXAMPLE OF VIEW MODEL - how does data persist using this view model after configuration changes

The ViewModel class is designed to store and manage UI-related data in a lifecycle conscious way. The ViewModel class allows data to survive configuration changes such as screen rotations.

Two examples has been shown

  1. Data saved in activity
  2. Data saved across fragments

OUTPUT

view modle data persist after the config changes as well ie. data is not lost after screen rotation

1. Data saved in activity

  1. Create MyViewModel which is data source, after changing in data source however activity get rotates but data is available on activity
public class MyViewModel extends ViewModel {
    private MutableLiveData<List<User>> users;

    public LiveData<List<User>> getUsers() {
        if (users == null) {
  1. Get this data in activity
// Create a ViewModel the first time the system calls an activity's onCreate() method.
        // Re-created activities receive the same MyViewModel instance created by the first activity.

        // BELOW FOR JUST FOR DEMO PURPOSE
        MyViewModel model = ViewModelProviders.of(this).get(MyViewModel.class);

        // THIS KIND OF OBSERVER CAN BE ADDED ANY WHERE LIKE IN ACTIVITY OR FRAGMENT
        // ADD OBSERVER IN ON CHANGE
       /*
        model.getUsers().observe(this, new Observer<List<User>>() {
            @Override
            public void onChanged(@Nullable List<User> users) {

            }
        });*/
        // SEE LAMDA VARIATIONS OF SAME AS ABOVE
        model.getUsers().observe(this, users -> {
            // update UI
            Log.i(TAG, "onChanged.......users...." + users);
        });
        

2. Data saved across fragments

pass data between fragments

  1. Get data in master ie main fragment
// BELOW TWO ARE SAME LABDA FLAVORS HAS BEEN SHOWN
            MyViewModel model = ViewModelProviders.of(this).get(MyViewModel.class);
           /*
            model.getUsers().observe(getActivity(), new Observer<List<User>>() {
                @Override
                public void onChanged(@Nullable List<User> users) {

                }
            });*/

            model.getUsers().observe(this, users -> {
                // update UI
                Log.i(TAG, "onChanged users...." + users);
                recyclerView.setAdapter(new MyFragmenItemRecyclerViewAdapter(users, mListener));
            });
  1. Update data in shared view model after item being selected from master/ main frament
//SET SELECTED VALUE IN SHARED VIEW MODEL
      // THIS DATA IS PERSIST DURING CONFIGURATION CHANGES AS WELL
      SharedViewModel model = ViewModelProviders.of(this).get(SharedViewModel.class);
      model.select(item);
  1. Shared data is available after activity recreated on config changes in this case data has been shown on details framgnet UI
   SharedViewModel model = ViewModelProviders.of(getActivity()).get(SharedViewModel.class);

       model.getSelected().observe(getActivity(), new Observer<User>() {
           @Override
           public void onChanged(@Nullable User dummyItem) {
               Log.i(TAG, "onChanged.... " + dummyItem.toString());
               textView.setText(dummyItem.toString());
           }
       });
       

UI

device-2018-02-17-203945

device-2018-02-17-203835

References

https://developer.android.com/topic/libraries/architecture/viewmodel.html https://codelabs.developers.google.com/codelabs/android-lifecycles/#0

About

Data of android UI is saved during configuration changes of device using view model of life cycle aware components. Different kind of android life cycle aware components. By using these component an app code becomes robust, testable, and maintainable.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages