Android:返回上一个活动而不调用finish()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1624338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:21:39  来源:igfitidea点击:

Android: Return to previous activity without calling finish()

android

提问by Orr Matarasso

I have an android application with a LOT of activities, think of something like a book where every page is a new activity. The user can make changes in each activity, for example highlight certain texts with different colored markers etc. and it's crucial that I'll remember this information as long as the application stays alive (and I don't want/need to remember any of this when it's not). As I understand the best mechanism for storing this kind of information is via onSaveInstanceState(Bundle outState)and onCreate(Bundle)/onRestoreInstanceState(Bundle)rather than lets say, the Preferences mechanism. My only problem is that the user can navigate backwards to previous pages (Activities) and the only way i know of achieving this is by calling finish()which of course kills the current activity without calling onSaveInstanceState(Bundle outState)and even if it did call it, the next time I would launch an activity representing that page it would be an entirely new instance. So my question is: Is there a way to go back to the previous activity without calling finish()? or, is there a better way to save this information? maybe through static variables?

我有一个有很多活动的 android 应用程序,想想像一本书,每一页都是一个新活动。用户可以在每个活动中进行更改,例如用不同颜色的标记突出显示某些文本等,并且只要应用程序保持活动状态,我就会记住这些信息是至关重要的(并且我不想/不需要记住任何当它不是时)。据我所知,存储此类信息的最佳机制是通过onSaveInstanceState(Bundle outState)onCreate(Bundle)/onRestoreInstanceState(Bundle)而不是说,首选项机制。我唯一的问题是用户可以向后导航到前一页(活动),我知道实现这一目标的唯一方法是调用finish()这当然会在不调用的情况下杀死当前活动onSaveInstanceState(Bundle outState)即使它确实调用了它,下次我启动代表该页面的活动时,它将是一个全新的实例。所以我的问题是:有没有办法在不调用的情况下返回上一个活动finish()?或者,有没有更好的方法来保存这些信息?也许通过静态变量?

Thanks!

谢谢!

P.S. I know I could also implement my app differently so that not every page would have its own activity but this isn't the answer I'm looking for.

PS 我知道我也可以以不同的方式实现我的应用程序,这样不是每个页面都有自己的活动,但这不是我正在寻找的答案。

回答by Nguyen Minh Binh

Try to use startActivity() with flags such as Intent.FLAG_ACTIVITY_CLEAR_TOP. You could read the full story here: http://developer.android.com/reference/android/content/Intent.html#setFlags(int)

尝试使用带有 Intent.FLAG_ACTIVITY_CLEAR_TOP 等标志的 startActivity()。你可以在这里阅读完整的故事:http: //developer.android.com/reference/android/content/Intent.html#setFlags(int)

回答by ?mer

I had exactly the same problem and after I thought and tried a lot, IMHO I found the most feasible solution. I inherited the Activity and adds a static method to kill if exists only. So, instead of killing the activity whenever it exits. I kill it whenever it is called again. For example,

我遇到了完全相同的问题,经过多次思考和尝试后,恕我直言,我找到了最可行的解决方案。我继承了 Activity 并添加了一个静态方法来杀死仅存在的情况。因此,不要在活动退出时终止活动。每当它再次被调用时,我都会杀死它。例如,

MyActivity.killIfExists();
startActivity(new Intent(this, MyActivity.class));

So that, the app will always be singleton and save its state with onSaveInstanceState.

这样,应用程序将始终是单例并使用 onSaveInstanceState 保存其状态。