Android 返回 MainActivity 的按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11460896/
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
Button to go back to MainActivity
提问by Moussa
I want to create a button that would lead the user straight back to main activity which doesn't have the android name="com.example.example".
It has android.intent.etc...
How can I reference my button to go back to this activity?
我想创建一个按钮,引导用户直接返回没有 android name="com.example.example" 的主要活动。
它有 android.intent.etc...
我怎样才能引用我的按钮返回到这个活动?
回答by zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
Lets say your main activity is called Main.java.
假设您的主要活动称为 Main.java。
btnBack.setOnClickListener(new OnClickListener(){
private void onClick(){
Intent intent = new Intent(currentActivity.this, Main.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
});
回答by K_Anas
use startActivity(intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
用 startActivity(intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
回答by Adam Stelmaszczyk
Sometimes you can just call activity.finish()
to end current activity, so the main (first created) activity will pop out.
有时您可以调用activity.finish()
结束当前活动,因此主要(第一个创建的)活动会弹出。
If this is not your case, do this:
如果这不是您的情况,请执行以下操作:
Intent intent = new Intent(getApplicationContext(), Main.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
startActivity(intent);
回答by confucius
Intent intent = new Intent(this, Main.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
回答by Jupiter
public void onBackPressed(){
finish();
}
回答by Code Droid
Well from wherever you are just call startActivity() with the required parameters inside the buttons onClick method. That's it.
无论您身在何处,只需在 onClick 方法中的按钮中使用所需的参数调用 startActivity() 即可。就是这样。