Android Activity堆栈和返回函数问题

发布于 2024-11-19 04:49:00 字数 90 浏览 4 评论 0原文

我有四个活动,A、B、C 和 D。应用程序从活动 A 开始,然后通过使用明确的意图转到 B,然后以相同的方式转到 C,然后 D。从D,如果我想直接回到B或A,怎么办?

I have four activities, A, B, C and D. app starts with activity A, then it goes to B by using explicit intend, then C and then D in same way. From D, if I want to come back to directly B or A, how it can be done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一绘本一梦想 2024-11-26 04:49:00

例如,在 C 中。如果您在发送启动 Activity D 的意图后调用 finish(),当用户在 Activity D 中按下后退按钮时,她/他将被发送到 Activity A 或 B,具体取决于您启动 Activity C 的位置。
另一种方法是设置标志以清除顶部,如下所示:

Intent intent  = new Intent(this, LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);        
startActivity(intent);

最简单的方法是根据您希望应用程序中的流程的方式调用 finish() 。

For example, in C. If you call finish() after you send the intent to start Activity D when the user presses the back button in Activity D she/he will be sent to Activity A or B depending where you started activity C.
Another way is to set flag to clear top like this:

Intent intent  = new Intent(this, LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);        
startActivity(intent);

The easiest way is to call finish() depending on how you want the flow in your app to be.

俏︾媚 2024-11-26 04:49:00

在活动A中

公共无效onCreate()
{
//当你想开始新活动时
启动活动(意图); //开始B的活动
}

在活动 B 中

公共无效onCreate()
{
//当你想开始新活动时
startActivityForResult(intent, 10//任意代码值); //启动c
活动
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(结果代码==25)
{
完成();
}
}

在活动 C 中

公共无效onCreate()
{
//当你想返回活动 A 时
setResult(25);
完成();
}

解释

在活动“A”中,您开始一项新活动到“B”
2)在“B”活动中,使用 startActivityForResult 方法将一项活动启动到“C”
3)在“C”活动中,当您完成时,它显然会转到“B”活动,并带有您设置的结果代码。如果匹配,它将关闭“B”活动,并转到“A”活动

4)这是跳过一项或多项活动的一个简单技巧

in Activity A

public void onCreate()
{
//when u want to start new activity
startActivity(intent); //starting activity to B
}

in Activity B

public void onCreate()
{
//when u want to start new activity
startActivityForResult(intent, 10//any code value); //starting activity to c
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(resultCode==25)
{
finish();
}
}

in Activity C

public void onCreate()
{
//when u want to go back to Actitvity A
setResult(25);
finish();
}

Explanation

1)in activity "A" ur starting one fresh activity to "B"
2)in "B" activity ,ur starting one activity to "C" ,using startActivityForResult method
3)in "C" activity ,when u finish,it obviously go to "B" activity,with result code which u set.and if it's match it will close "B" activity ,and go to "A" activity

4)This is one simple trick to skip one or more activity

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文