0

我有2个活动:A,B:
A会启动B。
但我想让B点击“返回”按钮可以返回桌面,但不能返回A活动。
我使用以下代码开始活动:

Intent NewActivity = new Intent();
NewActivity.setClass(A.this, B.class);
NewActivity.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(NewActivity);

但是在单击返回按钮时,它仍然返回到 A 活动。
我该怎么做?

4

6 回答 6

2

开始活动时添加finish()。

Intent NewActivity = new Intent();
NewActivity.setClass(A.this, B.class);
NewActivity.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(NewActivity);
finish();
于 2013-08-12T07:01:02.323 回答
1

那么我想你的目标是完成所有堆积起来的活动..

这里是 :-

关闭所有以前的活动如下:

Intent intent = new Intent(A.this, B.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.putExtra("Exit me", true); startActivity(intent); finish(); 然后在 B 的onCreate()方法中添加这个来完成 B

if( getIntent().getBooleanExtra("Exit me", false)){ finish(); }

否则,如果您只是从当前活动中返回,那么您的应用程序肯定会回到家中,因为您的堆栈是空的。

The result will be same as above, but because all your stacked up activities are closed, when you come back to you app it must start from your main activity i.e launcher activity.

Hope this helps.

于 2013-08-12T07:02:29.323 回答
1

Try this flag for this :

 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
于 2013-08-12T07:03:00.793 回答
0

当您开始新的活动时,您还可以在活动 A 中像这样完成()当前活动:

startActivity(new Intent(curentClass.this, newClass.class));
finish();
于 2013-08-12T07:00:15.857 回答
0

This is not a good thing to do. The typical user experience is, that they will go back to the previous activity, by pressing the back button. That is why there is a back- and home button on each device. The android guidelines states, that is the correct behaviour.

But if you want to do it anyway, you'll have to override the following method:

void onBackPressed()

That'll do the trick. Just start the MAIN Intent here, and you're done.

于 2013-08-12T07:02:58.437 回答
0

To get back to home screen ie desktop

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

finish() will just take u to your first screen ie A!

于 2013-08-12T07:26:22.570 回答