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

Thursday 21 May 2020

Preparing jar from Android studio library

Here is the code need to add in build gradle file to get jar file of your project module.

task makeJar(type: Copy) {    
    delete 'build/outputs/my-library-*.jar'    
    from('build/intermediates/aar_main_jar/release/')
    into('build/outputs/')
    include('classes.jar')
    rename('classes.jar', 'my-library-' + VERSION_NAME + '.jar')
}

makeJar.dependsOn(build)

Once you added the above code, sync the gradle file and click on Gradle option, which is there in right side and select the module project.
Tasks --> other --> makeJar(double click this)
Then it will start the process to generate and copy jar to "build/output/" folder.
Please go through the below images for better understanding.
* If you want to copy the jar to outside folder add "../" before the folder name
Example: into('../JarLibraryFolder/')
The above line will copy the jar file in "JarLibarayFolder" folder which is in project.