Friday, December 1, 2017

Persistent -Non Persistent In Android

Persistent and Non-Persistent in android:

Issue :
         I had met an problem,When the android mobile sleep and wake up time once again activity restart (ie) call onCreate() method,In my onCreate() method i had called the api to get the data and put my mobile as well don't disturb it automatically go to the sleep state the i had click the power button to wake up device in that time once again its called onCreate() method .


Solution: 

Play with the manifest to achieve this and also understand what exactly activity is, in Android eco system,In Android activity is a task which has a pre defined work. I found that, we can configure activity in two ways,
  • Persistent
  • Non persistent
if you mention for the activity in the manifest as

android:persistent="true"

and run the below use case

Start the APP
Press back or home button
you select the activity in the back stack again to bring it to front
Activity enters start -> pause -> stop - > resume , it does not get into onDestroy method.

if do not mention

android:persistent="true"
for the same use case

Activity enters start -> pause -> stop -> destroy, and if you select the activity from the back stack.

Activity enters resume->create->start

If you want to run a service/task on activity start which keeps running when the app is in back stack, then you have to start that in the onCreate method, and kill them onDestroy by specifying your activity as persistent in manifest.

I hope my above solution might help others who arrive here for the same problem

No comments:

Post a Comment