如何实现在单击后返回“Home”的Button?

时间:2016-01-24 07:16:32

标签: java android button

我在android studio中创建了一个“按钮”来发送电子邮件。当我点击它时,它会按预期发送一封电子邮件,但不会返回“主页”活动。

2 个答案:

答案 0 :(得分:0)

您可以使用finish();方法,但不建议这样做。

请参阅: How to finish current activity in Android

http://developer.android.com/guide/components/processes-and-threads.html#Threads

但是,这是我的建议,您应该可以在点击Button之后,然后清除活动并重新启动它(或者您可以执行类似Gmail App之类的操作并在点击后执行一些操作):

Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackageName());
                                        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                        startActivity(i);

您也可以使用:

Intent m = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(m);

或者:

startActivity(new Intent(FirstActivity.this, SecondActivity.class));

答案 1 :(得分:0)

Intent intent = new Intent(context, HomeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();