简体   繁体   English

意向和活动重新回到Android

[英]Intent and activity back on Android

I have 2 activities: A, B: 我有2个活动:A,B:
The A will launch B. A将启动B。
But I want to let B click "Back" button can back to desktop, but not back to A activity. 但是我要让B单击“返回”按钮可以回到桌面,但不能回到A活动。
I use below code to start activity: 我使用下面的代码开始活动:

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

But while click back button, it still back to A activity. 但是在单击“后退”按钮时,它仍然返回到A活动。
How can I do it? 我该怎么做?

Add finish() when you start the activity. 开始活动时添加finish()。

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

Then I suppose you are aiming at finishing all the stacked up activity.. 然后,我想您的目标是完成所有堆叠的活动。

Here it is :- 这里是 :-

Closing all the previous activities as follows: 关闭以前的所有活动,如下所示:

Intent intent = new Intent(A.this, B.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.putExtra("Exit me", true); startActivity(intent); finish(); Then in B's onCreate() method add this to finish the B 然后在B的onCreate()方法中添加它以完成B

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

or else if you simply going back from present activity, definitely your application goes to home just becz your stack is empty. 否则,如果您只是从当前活动中返回,则肯定是您的应用程序回到家,只是因为您的堆栈为空。

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 ie launcher activity. 结果将与上面相同,但是由于所有堆叠活动都已关闭,因此当您返回应用程序时,它必须从您的主要活动(即启动器活动)开始。

Hope this helps. 希望这可以帮助。

为此尝试此标志:

 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

When you have started the new activity, you can also finish() the current activity like this in activity A: 开始新活动后,还可以在活动A中像这样完成()当前活动:

startActivity(new Intent(curentClass.this, newClass.class));
finish();

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. android准则指出,这是正确的行为。

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. 只需在这里启动MAIN Intent,就可以完成。

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! finish()只会将您带到您的第一个屏幕,即A!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM