开发者

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

开发者 https://www.devze.com 2023-04-05 03:39 出处:网络
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

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 Ba开发者_如何学运维ckButton 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


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


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


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


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()

0

精彩评论

暂无评论...
验证码 换一张
取 消