开发者

How to not destroy current activity when back is presse

开发者 https://www.devze.com 2023-03-17 05:21 出处:网络
I couldn\'t find any answers on this, and I am a little lost.I have two activities, Act1 and Act2.I have a button in Act1 that starts a new intent to Act2 when it is pressed.However whenever i hit the

I couldn't find any answers on this, and I am a little lost. I have two activities, Act1 and Act2. I have a button in Act1 that starts a new intent to Act2 when it is pressed. However whenever i hit the back button while in Act2, than click the开发者_StackOverflow中文版 button again is restarts a new Intent.

i do not want it to do that... i want it to resume Act2. How can I go about doing it? Thanks in advance/


You could set the launch mode to "singleTop" as described in the docs and catch the back button presses to start a new Intent, that will reveal the old activity because they are singleTop.

<activity android:name=".Act1" android:launchMode="singleTop"/>
<activity android:name=".Act2" android:launchMode="singleTop"/>


It is android default behavior when you click on return(escape) or back button,it finishes the current activity and loads the previous activity.If you do not want the default behavior you can override the onKeyDown method it will help you.See the example code in the following doc URL http://developer.android.com/reference/android/app/Activity.html Are you doing any network or similar operations in Act2 which you do not want do next time?


You could override the onPause method in Act2 and save your info in a SharedPreferences object, which would be retrieved in the onCreate() on returning.

0

精彩评论

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

关注公众号