Coder Social home page Coder Social logo

dinkar1708-zz / lifecycle-aware-components Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 1.0 135 KB

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.

Java 100.00%
lifecycle-aware lifecycleobserver lifecycleowner android config modelview persistence rotation-device

lifecycle-aware-components's Introduction

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

lifecycle-aware-components's People

Contributors

dinkar1708 avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

winterdl

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.