Android的动作条上按钮对系统后退按钮 [英] Android Actionbar Up button versus system Back button
问题描述
我使用的动作条和它的向上按钮,从一个细节的活动返回到主活动,工作正常。同样,用户可以preSS系统的后退按钮返回到主活动。
I'm using the Actionbar and it's "up" button to return from a detail activity to the main activity, which works fine. Similarly, the user can press the system "back" button to return to the main activity.
在我的主要活动,在的onCreate()
数据从互联网上下载,显示在应用程序启动。我注意到,当我使用的动作条向上按钮,从细节到主要活动,的onCreate()
运行,重新下载数据。但的onCreate()
当我使用系统的后退按钮,所以立即显示出的主要活动视图不运行。
In my main activity, in onCreate()
data is downloaded from the internet to display upon app start. I noticed that when I use the Actionbar "up" button to go from detail to main activity, onCreate()
is run, re-downloading the data. But onCreate()
is not run when I use the system "back" button, therefore immediately showing the main activity view.
在code我在细节活动使用来实现向上按钮:
The code I use in the detail activity to implement the "up" button is:
switch (item.getItemId()) {
case android.R.id.home:
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
我想向上按钮的行为类似于后退按钮,而不是重新运行的onCreate()
。但我知道如何做到这一点,或者code路径中的后退按钮工具,返回的主要活动。
I would like the "up" button to behave like the "back" button and not rerun onCreate()
. But I'm unsure how to make this happen, or which code path the "back" button implements to return to the main activity.
谢谢!
推荐答案
而不是开始一个全新的活动只是完成细节的活性你在
Instead of starting a whole new activity simply finish the details activity you are in
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
然后你将返回到上的活动堆栈中的previous活动(你的主要活动)和的onCreate不应该叫
Then you will return to the previous activity on the activity stack (your main activity) and onCreate shouldn't be called
这篇关于Android的动作条上按钮对系统后退按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!