-7

I want to know what happens when back button pressed, is there any data loss? Or we simply press back button what could be possibly done to that state or there is data loss when we press home button, and lastly when we remove application from recent app, what happens when we remove application from recent app is there a data loss or something

Thanks!

blackHawk
  • 6,047
  • 13
  • 57
  • 100

2 Answers2

3

As far as I can understand, you want to know that what does happen with application data when following actions performed:

  1. When exits from Application using device back button
  2. When application goes background after tapping Home button
  3. When application removed from recent application list

1. When exit from Application using device back button

When user exits from certain application then all the activities of that application are being removed from ativity stack and all related data also being removed from stack and heap by android system. If user expect some other behaviour then developer needs to override the onBackPressed() method.

2. When application goes background after tapping Home button

When application goes background using Home button then activities of that application are keeped in back-stact by activity manager. Preserving application data is dependent on how developer handled them in onStop() method.

3. When application removed from recent application list

In this case, all the activities of that application are being removed from ativity stack and all related data also being removed from stack and heap by android system. If user expect some other behaviour then developer needs to override and customize the onStop()/onBackPressed() method.

I hope this little explanation will help you.

Hamid Shatu
  • 9,664
  • 4
  • 30
  • 41
  • Thats helps, what about services, alarms job schedulers, are only local variables losses its data? What about static variables – blackHawk Jul 24 '17 at 06:31
  • For services, it also depends on dev. If dev wants to keep his service running even after application closed from foreground then data handled by service will not be lost. Static data is like global data, so until all the process of the application are closed, static data will be remain same. And local variables values will behave according to their scope. For more clear understanding, you need to have clear understanding on life-cycle of Activity and Service – Hamid Shatu Jul 24 '17 at 06:39
  • could you delete your answer im having too much downvote – blackHawk Jul 24 '17 at 08:40
  • I think you should re-organize your question more, so that people can understand the question at a glance. – Hamid Shatu Jul 24 '17 at 08:55
  • downvote wont go, could you suggest edit? – blackHawk Jul 24 '17 at 09:15
2

Pressing 'Home button' from an activity/fragment will lead to onPause() then onStop() that because of the lifeCycle of Android, that perform those states when activity is no longer visible.

Pressing back button when in activity, will lead to onPause-> onStop() ->onDestroy(), because the back button will call to finish() of the activity. from within a fragment, it will invoke the onBackPressed() method of the host activity (and there you can decide what to do).

Removing from recent apps the application will lead to onDestroy() since you are terminating the process.

NOTE: There is a VERY useful tutorial from Vogella here you can learn from a lot about the lifeCycle.There, you'll find a very simple POC with notifications that show you the whole lifeCycle visually.

Hope it helps :)

BNAndroid
  • 158
  • 4