如何在 Android 中将父 Activity 置于前台?

发布于 2024-09-18 05:28:16 字数 538 浏览 5 评论 0原文

众所周知,startActivityForResult() 用于从父活动获取任务结果。当我们点击setResult()时。它将结果返回给父 Activity。

子活动意味着它应该保持持久状态。即当用户单击子活动上的按钮时。它不会完成该活动并显示父活动。我可能会再次从父活动转到子活动。如果子活动不存在,它将启动一个新的子活动,否则它应该显示具有持久内容的现有子活动。

编辑:

简单地说,我的父活动启动了一个新的子活动。现在这两个 Activity 都应该处于活动状态。如何在不完成子活动的情况下调用父活动(换句话说,如何将父活动带到前面)?对于这种情况,

  • 您不能使用startActivity()。因为它创建了一个新的 Activity 实例。不存在父 Activity,
  • 您无法使用 startActivityForResult()。因为它完成了子 Activity 实例。

在 Android 中这可能吗?有什么想法吗?

As we know, startActivityForResult() used to get a result for a task from parent activity. Here when we click the setResult(). It returns the result to the parent Activity.

Child Activity means it should maintain the persistent state. That is when the users clicks a button on the child activity. It do not finishes that activity and show the Parent Activity. And again i may go to the child activity from parent activity. If the child does not exists it starts a new child activity else it should show the existing child activity with persistent content.

Edit:

Simply said, my parent Activity starts a new Child Activity. Now both Activities should be alive. How to call the Parent Activity without finishing the child Activity(in other words, How to BringtoFront the parent Activity)? For this scenario,

  • you can not use startActivity(). Because it creates a new Activity Instance. Not Existing Parent Activity
  • you can not use startActivityForResult(). Because it finishes the child Activity Instance.

Is that possible in Android? Any Idea?

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

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

发布评论

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

评论(3

情话墙 2024-09-25 05:28:16

这在 Android 中是不可能的。活动生命周期中不提供这种情况。

可以通过重用单个活动并仅切换布局来模拟这种行为。您必须存储活动的整个状态,然后将布局更改为子活动的布局,如果用户切换回父活动,您将必须保存子活动的状态并恢复状态家长的活动。您还需要自己处理后退键,以防止用户按下它并返回到启动父活动的活动。

一种更简单的方法是使用 tabwidget 并让用户在选项卡之间切换。这使您能够使用两个不同的意图初始化选项卡,并且只有当用户更改回另一个选项卡时,这两个选项卡才会暂停。

This is not possible in Android. Such a case is not provided for in the activity life cycle.

It is possible to simulate such a behaviour through reusing a single activity and just switching the layout. You would have to store the whole state of the activity, then change the layout to the layout that should be the child activity and if the user switches back to the parent activity you would have to save the state of the children activity and restore the state of the parent activity. You would also need to handle the back key yourself to prevent the user to press it and go back to the activity that started the parent activity.

A simpler way would it be to use a tabwidget and let the user switch between to tabs. This enables you to initialize the tabs with two different intents and both of them will be only paused if the user changes back to another tab.

幽蝶幻影 2024-09-25 05:28:16

请为父 Activity 和子 Activity 设置不同的 taskAffinity
并使用 startActivity(intent) 通过 FLAG_ACTIVITY_NEW_TASK 启动您的活动旗帜。

该方法导致两个活动属于不同的任务,并且当您通过添加 FLAG_ACTIVITY_NEW_TASK 标志切换任务时状态将保持。

我认为应该有效。

Please set different taskAffinity to parent and child activities,
and use startActivity(intent) to start your activities with FLAG_ACTIVITY_NEW_TASK flag.

The method lead to two activities belong to different task, and the state will be keep when u switch task by adding FLAG_ACTIVITY_NEW_TASK flag.

I think it should work.

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