如何在转到下一个活动时销毁前一个活动,但不包括闪屏和主页活动?

huangapple go评论72阅读模式
英文:

How to destroy the previous activity but splash and home activity is not included when moving to the next activity?

问题

我有 6 个打开的活动(见下面的图片)。

但是我想要的情况是,当用户进入 orderDetailsActivity 时,它会销毁所有先前的活动,但不包括 splashActivityhomeActivity

因此,用户可以轻松返回到 homeActivity。请注意,我只想在返回到 homeActivity 时使用 finish();

注意: 用户可以在 homeActivitycartActivitycheckoutActivity 之间来回切换,因此在切换到另一个活动后立即销毁这些活动是不可能的。

英文:

I have 6 open activities (see the image below).

如何在转到下一个活动时销毁前一个活动,但不包括闪屏和主页活动?

But what I want to happen is when the user proceeds to orderDetailsActivity it will destroy all the previous activity but not the splashActivity and homeActivity.
如何在转到下一个活动时销毁前一个活动,但不包括闪屏和主页活动?

So the user can easily move back to homeActivity. And please note that I want to use finish(); when moving back to homeActivity only.

如何在转到下一个活动时销毁前一个活动,但不包括闪屏和主页活动?

Note: The user can move back and forth from homeActivty, cartActivity and checkoutActivity so destroying those activities right after moving to another one is not possible.

答案1

得分: 1

结账活动(Checkout Activity)切换到加载活动(Loading Activity)时,您可以简单地调用finish()来销毁结账活动

加载活动onBackPressed方法中,您可以像这样操作:

Intent intent = new Intent(Loading.this, Home.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
finish();

这将使您重定向到主页活动(Home Activity)(先前的实例),并且将清除堆栈顶部的活动。因此,您想要的效果已实现!

了解更多信息:了解任务和返回堆栈

更新:##

如果您希望在订单详情活动(Order Details Activity)中实现相同的效果,只需将代码放在订单详情onBackPressed方法中:

Intent intent = new Intent(OrderDetails.this, Home.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
finish();
英文:

When you move from Checkout Activity to Loading Activity you can simply call finish() to destroy the checkout activity.

In the onBackPressed of Loading Activity you can do like:

Intent intent = new Intent (Loading.this , Home.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.ACITIVTIY_SINGLE_TOP);
startActivity(intent);  
finish();

This would redirect you to the Home Activity (previous instance) and will clear the activities which are on top of the stack. Therefore, what you want is achieved!

For more info: Understand Tasks and Back Stack

Update:

If you want the same thing to happen in the Order Details Activity, you just put the code in onBackPressed of order details:

Intent intent = new Intent (OrderDetails.this , Home.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.ACITIVTIY_SINGLE_TOP);
startActivity(intent);  
finish();

答案2

得分: 0

你可以使用finish()

在你的导航行中加入finish()

 Intent it = new Intent(currentActivity.this, MovingActivity.class);
 startActivity(it);
 finish();

而对于不想销毁的活动,只需添加

 Intent it = new Intent(currentActivity.this, MovingActivity.class);
 startActivity(it);
英文:

You can use finish()

In your navigation line you add finish()

 Intent it = new Intent (currentActivity.this,MovingActivity.class);
 startActivity(it);
 finish();

and which activity not want to destroy their you add only

 Intent it = new Intent (currentActivity.this,MovingActivity.class);
 startActivity(it);

huangapple
  • 本文由 发表于 2020年9月24日 11:47:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/64039245.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定