英文:
How to destroy the previous activity but splash and home activity is not included when moving to the next activity?
问题
我有 6 个打开的活动(见下面的图片)。
但是我想要的情况是,当用户进入 orderDetailsActivity
时,它会销毁所有先前的活动,但不包括 splashActivity
和 homeActivity
。
因此,用户可以轻松返回到 homeActivity
。请注意,我只想在返回到 homeActivity
时使用 finish();
。
注意: 用户可以在 homeActivity
、cartActivity
和 checkoutActivity
之间来回切换,因此在切换到另一个活动后立即销毁这些活动是不可能的。
英文:
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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论