活动中的线程在启动另一个线程时是否会终止?

发布于 2024-12-21 22:52:38 字数 163 浏览 5 评论 0原文

我有一个启动 3 个线程的活动 当我开始一个新活动时, “While(true)”循环

,这些线程是否会自行终止?

另一个问题,我如何开始一项新活动,但我不想允许 “后退”按钮让我返回到旧的活动 - 实际上我想处理第一个活动并创建并显示一个新的活动

谢谢 罗恩

I have an activity that starts 3 threads that make a
"While(true)" loop

when i start a new activity, does those threads terminate themselves?

And another question, how do i start a new activity but i don't want to allow
the 'back' button to return me to the old activity -
actually i want to dispose the first activity and create and show a new one

thank you
Ron

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

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

发布评论

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

评论(2

雨巷深深 2024-12-28 22:52:38

当我开始一个新活动时,这些线程会自行终止吗?

不,他们没有。您必须在结束活动之前阻止它们。

如何开始新活动,但我不想让“后退”按钮返回到旧活动

只需在启动第二个活动时从第一个活动中调用 finish() :

Intent intent = new Intent(...);
startActivity(intent);
finish();

when i start a new activity, does those threads terminate themselves?

No, they don't. You have to stop them before closing your activity.

how do i start a new activity but i don't want to allow the 'back' button to return me to the old activity

Just call finish() from your first activity when you start the second one :

Intent intent = new Intent(...);
startActivity(intent);
finish();
伴梦长久 2024-12-28 22:52:38

当活动暂停或停止时,线程可能不会立即停止。更好的策略是在 onPause 或 stop 被调用时立即将一个标志设置为 false。
线程检查此标志并中断自身或退出 run 方法。

如果您不希望后退按钮显示较旧的 Activity,可以对较旧的 Activity 使用 NO_HISTORY 标志 http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_NO_HISTORY

如果您想将此 Activity 作为堆栈中的第一个活动使用此标志 FLAG_ACTIVITY_TASK_ON_HOME

When activity pauses or stops Threads might not be stopped immediately. Better strategy is to have a flag which is set to false as soon as onPause or stop is called.
Threads check for this flag and interrupt themselves or come out of run method.

If you don't want back button to show older Activity, you can use NO_HISTORY flag for the older activity http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_NO_HISTORY

If you want to have this activity as the first activity in the stack use this flag FLAG_ACTIVITY_TASK_ON_HOME

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