startNewActivity生命周期多次错过理解

发布于 2024-12-09 13:49:56 字数 884 浏览 0 评论 0原文

在 oncreate 中的活动 A 的 oncreate 中

while(i<5){
startActivity(new Intent(this, ActivityB.class));
Log.v(tag,"activity A");
}

,在 oncreate 中的活动 B 中

Log.v(tag,"activity B");
finish();

,然后在控制台中,我看到类似这样的内容

activity A
activity A
activity A
activity A
activity A
activity B
activity B
activity B
activity B
activity B

,如果输出是这样的情况,我应该更改什么

activity A
activity B
activity A
activity B
activity A
activity B

,我的意思是对于每个 startActivity 方法,我希望实际启动新活动。因为活动 B 实际上只存在从开始到结束的一小部分时间。我的问题在于理解活动生命周期。我预计当我调用 srartActivity 时,活动确实会启动,但它没有发生。

编辑1: 好的,我只粘贴了一小段代码。问题不在 while 循环中,在我的实际项目中,通过 i 变量的初始化(如 int i-0; )正确编写了并在循环内递增,如 i++; 。 。 。正如您所看到的,应用程序的输出很好,所有内容都会被调用多次,依此类推,但问题在于调用的顺序。就像 startActivity 在调用 srartActivity 后不会启动该活动,而是在 ActivityA 的 oncreate 完成后启动所有事情。

in oncreate of activity A

while(i<5){
startActivity(new Intent(this, ActivityB.class));
Log.v(tag,"activity A");
}

in activity B inside oncreate

Log.v(tag,"activity B");
finish();

and then in the console I see something like this

activity A
activity A
activity A
activity A
activity A
activity B
activity B
activity B
activity B
activity B

What should I change to have situation where the output is like this

activity A
activity B
activity A
activity B
activity A
activity B

I mean for every startActivity method I want new activity to actually to be started. Cause the activity B actually lives for a fraction of moment it is started and than finished. My problem is in the understanding the activity lifecycle. I expected that when I call srartActivity the activity to be started indeed, but it doesn't happens.

Edit1:
ok I pasted just a small snippet of my code. and the problem is not in the while loop, in my real project is wrote correctly with initialization of the i variable like int i-0; and incrementing inside the loop like i++; . . . As you can see the output from the app is good, everything is get called in a sertian number of times and so on, but the problem is the order of calling. it is like startActivity doesn't start the activity after the call of the srartActivity, instead every thing is started after oncreate of activityA is finished.

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

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

发布评论

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

评论(1

清风挽心 2024-12-16 13:49:56

这不仅与 Activity 生命周期有关,还与应用程序主线程(= UI 线程)中的消息处理有关。
startActivity 在发送 Intent 后立即返回,因此 while 循环会在很短的时间内触发 5 个 Intent 来启动 Activity B,然后将控制权返回给线程的消息循环。
仅在从当前方法调用返回后,您的应用程序主线程才能开始处理先前生成的消息,这将导致创建 Acitvity 5 的 5 个实例,同样是一个接一个。

如果您希望 Activity A 推迟到 Activity B 完成,您可以调用 startActivityForResult - 一次!。然后,在 Activity B 中调用 finish(int) 将结果返回给 Activity A。覆盖 onActivityResult 在 Activity A 中获取结果代码,如果需要,您可以从这里再次启动 Acitity B。

另请参阅启动 Activity 并获取结果

This is not just about activity lifecycle, but also about the message processing in the main thread (=UI thread) of your app.
startActivity returns immediately after sending the intent, so the while loop fires 5 intents to start Activity B within a very short time before returning control to the thread's message loop.
Only after returning from the current method call, your applications main thread can start processing the previously generated messages that will result in creating the 5 instances of Acitvity 5, once again one after the other.

If you want Activity A to hold off until Activity B has finished, you can call startActivityForResult - once!. Then, in Activity B, call finish( int) to return a result to Activity A. Override onActivityResult in Activity A to get the result code and from here you can again start Acitity B if needed.

See also Starting Activities and Getting Results

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