-2

I have a strange behavior in my app when running in jelly bean 4.1.1 , when i start an Activity and press the back button to go back to the First Activity it re-create it by calling onCreate also , what i did also i handle the configuration change with the first activity so it won't recreated on configuration change but thats didnt make anything . !!!! why Activity life cycle in jelly bean have a strange behavior. You can see what i mean , just create a simple Android project and create two activity , navigate from the first one to activity to and press back button and see the logs onCreate on Activity 1 will be called !!!!

user4o01
  • 2,688
  • 4
  • 39
  • 54
  • Are you talking about Fragments here? Because they COULD get recreated at times. That is, onCreateView will get called. However, the main activity should follow the Android standard. – Edison Sep 07 '12 at 17:32
  • I do not have this behavior in any of my Jelly Bean apps. – Cat Sep 07 '12 at 17:36
  • How are you "navigating" between activities right now? If not under low memory condition, navigating back should always call onResume only. I don't have this behavior in any of my 4.1 apps as well. You should track when onDestroy is called. – Edison Sep 07 '12 at 17:38
  • 1
    There is also a developer option that destroys apps as soon as you leave them. Make sure this isn't checked. – Cat Sep 07 '12 at 17:43

3 Answers3

4

There is no guarentee that the OS won't kill your background Activities at any time if it determines that it needs the resources. Just a guess, but onCreate may get called in these cases because the system kills the first Activity before you get back to it.

Edit: http://developer.android.com/guide/components/activities.html#Lifecycle

If an activity is paused or stopped, the system can drop it from memory either by asking it to finish (calling its finish() method), or simply killing its process. When the activity is opened again (after being finished or killed), it must be created all over.

MattDavis
  • 5,158
  • 2
  • 23
  • 35
1

Here is another reference for you:

http://developer.android.com/training/basics/activity-lifecycle/recreating.html http://developer.android.com/training/basics/activity-lifecycle/starting.html

In the first link:

The system may also destroy your activity if it's currently stopped and hasn't been used in a long time or the foreground activity requires more resources so the system must shut down background processes to recover memory.

...

However, if the system destroys the activity due to system constraints (rather than normal app behavior), then althought the actual Activity instance is gone, the system remembers that it existed such that if the user navigates back to it, the system creates a new instance of the activity using a set of saved data that describes the state of the activity when it was destroyed. The saved data that the system uses to restore the previous state is called the "instance state" and is a collection of key-value pairs stored in a Bundle object.

Joel Slowik
  • 653
  • 5
  • 13
1
  1. Open Phone setting screen
  2. click on developer option(if not present then press about phone 5-6 times)
  3. In developer option in App category uncheck Do not keep Activities flag
laalto
  • 150,114
  • 66
  • 286
  • 303
Rinkesh
  • 3,150
  • 28
  • 32