简体   繁体   English

按下后退时如何不破坏当前活动

[英]How to not destroy current activity when back is presse

I couldn't find any answers on this, and I am a little lost.我找不到任何答案,我有点迷茫。 I have two activities, Act1 and Act2.我有两个活动,Act1 和 Act2。 I have a button in Act1 that starts a new intent to Act2 when it is pressed.我在 Act1 中有一个按钮,当它被按下时,它会启动 Act2 的新意图。 However whenever i hit the back button while in Act2, than click the button again is restarts a new Intent.但是,每当我在 Act2 中按下后退按钮时,再次单击该按钮都会重新启动一个新的 Intent。

i do not want it to do that... i want it to resume Act2.我不希望它这样做...我希望它恢复 Act2。 How can I go about doing it?我该怎么做go呢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.您可以按照文档中的说明将启动模式设置为“singleTop”,然后按下后退按钮以启动新的 Intent,这将显示旧的活动,因为它们是 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?它是 android 当您单击返回(退出)或返回按钮时的默认行为,它完成当前活动并加载上一个活动。如果您不想要默认行为,您可以覆盖 onKeyDown 方法,它将帮助您。参见示例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.您可以覆盖 Act2 中的 onPause 方法,并将您的信息保存在 SharedPreferences object 中,返回时将在 onCreate() 中检索。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM