Android 应用程序/活动每次启动或恢复时都完全重新启动?

发布于 2024-10-03 18:03:38 字数 443 浏览 0 评论 0原文

我有一个适用于 Android 的儿童应用程序,该应用程序有一些独特的考虑因素,因为该应用程序基本上没有导航(适合年幼的孩子)。我不想通过添加退出/重新启动按钮来破坏我的应用程序 UI(在 iPhone 上已经成功)。

我真正需要的是相当简单的——我希望我的活动/应用程序每次启动时都能干净、全新地启动。无论是初始加载还是其他什么 - 基本上任何时候调用 onResume 时我都想要我的应用程序的全新实例。

我最初以为我可以在用户离开时退出/退出/完成应用程序。但我还没有找到一种方法来做到这一点,并且不会导致启动时崩溃。此外,关于这个想法的每个线程/堆栈溢出帖子都充满了人们摇着手指并说你永远不应该退出 Android 上的应用程序。

如果我无法在 onExit 上退出应用程序,是否可以在每次调用 onResume 时重新启动我的 Activity? (或者这会是一个无限循环?)。

非常感谢任何帮助!

I have a kid's app for Android and there are some unique considerations for this application since the app has basically no navigation (it's for young kids). I do not want to break my app UI (which has been successful on iPhone) by adding a quit/restart button.

What I really need is fairly simple -- I want my activity/app to start clean and new every single time it starts. Whether it's an initial load or whatever -- basically any time onResume is called I want a completely fresh instance of my app.

I initially thought I could just exit/quit/finish the app when the user leaves. But I haven't found a way to do this that doesn't cause crashes on start. Also every thread/stack overflow post about that idea is filled with people wagging their fingers and saying you should never ever quit an app on android.

If I can't quit the app onExit, is there something I can do to restart my activity every time onResume is called? (or would that be an infinite loop?).

Would greatly appreciate any help!

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

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

发布评论

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

评论(3

回梦 2024-10-10 18:03:38

尝试在 onResume 中启动您的主要活动,并清除活动堆栈:

public void onResume() {
    super.onResume();
    startActivity(new Intent(this, MainScreen.class).addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
}

也许这些不是要添加的正确标志,但检查其他 Intent 标志,这可以做到你想要什么!

意图标志文档

Try starting your main activity in onResume, and clearing the activity stack:

public void onResume() {
    super.onResume();
    startActivity(new Intent(this, MainScreen.class).addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
}

maybe these aren't the correctl flags to add, but check out the other Intent flags and this could do what you want!

intent flags documentation

转瞬即逝 2024-10-10 18:03:38

最终通过在 onPause() 中调用 finish() 使其正常工作。

再次,我很欣赏人们所说的“这不是 Android 做事的方式”的建议。目前我对最佳实践已经有了很好的了解。对于不寻常类型的用户来说,这是一种不寻常的情况。

Ended up getting it to work fine by calling finish() in onPause().

Again, I appreciate the advice from people saying "this is not how Android does things". I've got a pretty good understanding of the best practices at this point. This is an unusual situation for an unusual type of user.

沩ん囻菔务 2024-10-10 18:03:38

在你的重新加载中你可以尝试这个..

onStop();
onCreate(getIntent().getExtras());

in your reload you can try this..

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