Rafael T
Rafael T

Reputation: 15679

Android Activity-Lifecycle... how to avoid onDestroy()?

I have an App, which uses an ActicityGroup to have a stack of Activitys. If I start a new Activity, I do this as Child of my ActivityGroup. Lets assume I'm in my starting Activity (1), and I start a new one(2), so here is what's getting called:

(1): onPause()

(2): onCreate(), onStart(), onResume()

until here, everything as aspected. if I press my BackButton the stack is as following:

(2): onPause(), onStop(), onDestroy()

(1): onStop(), onDestroy() [sic]

(1): onCreate(), onStart(), onResume()

I see no reason,first why (1) should perform onStop, and onDestroy(), to reCreate again, and second why onRestart never gets called on (1).

Has anyone a Reason for this behavior? Can I 'cancel' calls to onStop() or onDestroy() somehow? any idea apreciated

Upvotes: 2

Views: 8992

Answers (4)

Aravind.HU
Aravind.HU

Reputation: 9472

You can avoid Activity onDestroy method when back button pressed in a very simple way,

Just mention

android:persistent="true"

for your activity, that should prevent your activity getting destroyed, To know more in detail you can visit the below SO post

Prevent activity from being destroyed as long as possible

Upvotes: 2

Tofeeq Ahmad
Tofeeq Ahmad

Reputation: 11975

Onstop() and onDestroy() will not call untill you did not finish activity. Ondestory() called for releasing of resource that occupied by Activity. IF activity use has been over then it is better to destroy this activity.

Second it will be good for memory management scheme. and if you do not call destroy then it will automatically call when you exit from app

and finally if you doesnot want to call destroy when you press back button then you can use override method onBackpressed()

Upvotes: 0

JohnUopini
JohnUopini

Reputation: 970

Try using FLAG_ACTIVITY_SINGLE_TOP when starting child activity, like:

    Window window = getLocalActivityManager().startActivity(id,
            intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP));

More info here:

http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_SINGLE_TOP

Upvotes: 5

Pratik
Pratik

Reputation: 30855

when you press back button it's default behavior is stop or destroy the current activity you can override the back button. If you press home button then it will use the onPause() that means os will consider that you want to continue with current activity when you again launch same activity but if you press back button it means that you finish your current activity and back to the last activity

Upvotes: 0

Related Questions