Android 在单击按钮时返回第一个活动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2776830/
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
Android moving back to first activity on button click
提问by cppdev
I am writing a application where I am dealing with 4 activities, let's say A, B, C & D. Activity A invokes B, B invokes C, C invokes D. On each of the activity, I have a button called "home" button. When user clicks on home button in any of the B, C, D activities, application should go back to A activity screen ?
我正在编写一个应用程序,我在其中处理 4 个活动,比方说 A、B、C 和 D。活动 A 调用 B,B 调用 C,C 调用 D。在每个活动上,我都有一个名为“主页”的按钮按钮。当用户在 B、C、D 活动中的任何一个中单击主页按钮时,应用程序应返回到 A 活动屏幕?
How to simulate "home" button in this case ?
在这种情况下如何模拟“主页”按钮?
回答by synic
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(D.this, A.class));
}
});
Declare A in your manifest with the android:launchMode="singleTask"
. This way, when you call startActivity()
from your other activies, and A is already running, it will just bring it to the front. Otherwise it'll launch a new instance.
在清单中使用android:launchMode="singleTask"
. 这样,当您startActivity()
从其他活动中调用并且 A 已经在运行时,它只会将其带到前面。否则它会启动一个新实例。
回答by vivek
The only problem i see using android:launchMode="singleTask"
is whenever you minimize app and start app by pressing app icon again then app start from scratch and doesn't acquire its state. so i have opted to use
我看到使用的唯一问题android:launchMode="singleTask"
是每当您最小化应用程序并通过再次按应用程序图标启动应用程序然后应用程序从头开始并且不获取其状态时。所以我选择使用
intent.setFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP );
which will keep only one instance and clear all activities on top of that.
这将只保留一个实例并清除其上的所有活动。
Intent intent = new Intent( context, MyActivity.class );
intent.setFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP );
current_activity.startActivity( intent );
回答by Chen Jiling
Kotlin
科特林
fun Context.popToRoot()
{
val intent = Intent(this, MainActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
startActivity(intent)
}
Change MainActivity to your main activiy.
将 MainActivity 更改为您的主要活动。
In any activity just call:
在任何活动中只需调用:
this.popToRoot()
回答by Tom Rumens
First of all I can't believe Android does not have an easy equivalent of iOS poptorootviewcontroller.
首先,我无法相信 Android 没有简单的 iOS poptorootviewcontroller 等价物。
The problem with using an intent and startactivity is that as far as I know it will recreate the root activity which is a problem if for example it is using an asynchronous network request to load data and layout the interface. This may have unpleasant visual effects.
使用意图和启动活动的问题是,据我所知,它会重新创建根活动,这是一个问题,例如,如果它使用异步网络请求来加载数据和布局界面。这可能会产生令人不快的视觉效果。
Here is a creative solution I have used in my friendfolder project:
这是我在我的朋友文件夹项目中使用的一个创造性的解决方案:
Create a global boolean: public boolean myBool = true;
Do this in MyApplication class and register the class in the manifest:
创建一个全局布尔值:public boolean myBool = true;
在 MyApplication 类中执行此操作并在清单中注册该类:
<application
android:name=".MyApplication"
Activity A is the root, do this in onResume:
Activity A 是根,在 onResume 中执行此操作:
if(((MyApplication) this.getApplication()).myBool == false) {
if(isTaskRoot()) {
((MyApplication) this.getApplication()).myBool = true;
} else {
finish();
}
}
In every activity that will go on top of Activity A put this code in:
在将在活动 A 之上进行的每个活动中,将此代码放入:
@Override
public void onResume() {
super.onResume();
if(((MyApplication) this.getApplication()).myBool == false) {
finish();
}
}
@Override
public boolean onSupportNavigateUp(){
((MyApplication) this.getApplication()).myBool = false;
finish();
return true;
}
You can even put this in activity A as well if you are stacking other instances of A on top of the root A. onSupportNavigateUp is for the up button. Substitute this method for another if using your own button.
如果您将 A 的其他实例堆叠在根 A 的顶部,您甚至可以将其放在活动 A 中。 onSupportNavigateUp 用于向上按钮。如果使用您自己的按钮,请将此方法替换为另一种方法。
Now when you have some activities stacked on top of A and you press the up button the top activity will finish and the onresume will be called by the system on the activity below in the stack and it will finish. This will work in a chain all the way back to the root A.
现在,当您将一些活动堆叠在 A 的顶部并按下向上按钮时,顶部活动将完成,系统将在堆栈中的下方活动上调用 onresume 并完成。这将在一个链中一直工作到根 A。
回答by Reivaj
This works for me
这对我有用
Intent intent = new Intent(D.this, A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
In the manifest:
在清单中:
android:name=".A"
android:launchMode="singleTask"
回答by progr150
I'm using https://github.com/greenrobot/EventBusfo this. In root activity you must subscribe for event
我正在为此使用https://github.com/greenrobot/EventBus。在根活动中,您必须订阅事件
EventBus.getDefault().register(this);
EventBus.getDefault().register(this);
Then you must define event callback
然后你必须定义事件回调
public void onEvent(LogoutEvent event) {
// your implementation
logout();
}
and the logout button listener must have code like this:
并且注销按钮侦听器必须具有如下代码:
finish(); // you must finish previous activity
EventBus.getDefault().post(new LogoutEvent());
That's all
就这样