Wednesday, 3 June 2020

Android keeping the same data after application orientation change.

This contains the sample code to show demo to keep the same data after changing the orientation of android application.

DemoActivity.java & DemoActivityViewModel.java: These 2 classes are demonstrating keeping data same after orientation change of android application using ViewModel library(Android Jetpack). To use this integration in your code you need to add the below dependencies.
    implementation 'android.arch.lifecycle:extensions:1.1.1'
  annotationProcessor 'android.arch.lifecycle:compiler:1.1.1'
Here I'm using version 1.1.1 but it may vary in future, please use the latest version always.

MainActivity.java:
This class is using onSaveInstanceState() method to save data before changing the orientation of application, then we can get the saved data in onCreate() method using savedInstanceState variable.

Source: video_1, video_2, link_1

No comments:

Post a Comment