STeN
STeN

Reputation: 6268

Keep activity in paused state - Android

I have an application with 3 tabs with one activity per tab. When I am switching between first two activities, the activity, which is going to background passes the onPause() state, while the new one becomes active and onResume() is called. That’s good, because both activities have a complex UI and rendering them takes 2-3 seconds, but when they are kept in paused state, they are resumed quickly.

But when I click on the 3rd tab, then the application behavior is different, the activity, which is going to background, is completely destroyed (it passes onPause(), onStop() and onDestroy()).

Any idea why there is difference in behavior? Is there a way to force the activity to remain in a paused state when user switches to another activity within the application?

Thanks STeN

Upvotes: 1

Views: 1206

Answers (2)

Shlublu
Shlublu

Reputation: 11027

This behaviour can even be different on another device or in any other context (different amount of available memory, etc).

You have the guarantee that

  • onResume() is called when an Activity is going foreground
  • onPause() is called when the Activity is going background,

and this is the only guarantee you have. (More details on the Activity lifecycle here).

So you can make absolutely no assumption about the calling of onStop() and onDestroy(). They may be called or not each time you are switching from a tab to another, and your application needs being able to handle this.

Upvotes: 4

NewProggie
NewProggie

Reputation: 1095

The reason why the first activity is destroyed may be caused by the lack of heap memory. If Android is running out of memory it kills background activities. This behaviour cannot be influenced by the developer since the whole system would slow down and crash if there's not enough memory left to keeps things going.

Upvotes: 1

Related Questions