点击后退按钮但不终止活动并让它进入后退堆栈(Android)
我有 3 个活动 A、B 和 C。
我通过单击按钮动态地扩展活动 B,用户可以添加任意数量的视图。
操作是这样的:
用户看到“活动 A”首先输入他的详细信息并单击“保存”按钮,我将他带到“活动 B”,在那里他可以多次添加某些字段,当他再次单击“保存”时,我将他带到“活动 B” “活动C”。
现在,当我在 A 处转到 B 并添加一些视图并在 TextViews 中输入文本,然后单击“保存”并转到 C 时。从 C 开始,如果我回击,我会看到 B 完好无损,以及所有膨胀的视图并输入文本,显然是因为它保存在返回堆栈中,但是如果我通过回击从 B 转到 A,然后返回到 B,则所有视图都会消失,因为它已从返回堆栈中删除。
我想知道是否有可能在 BackStack 中只保存一个 B 实例,并且当用户回击时根本不杀死它? 我已经覆盖了后退键,但无济于事,因为活动无论如何都会被终止,有些人建议我应该将整个视图和数据保存到 Parcelable ArrayList 中,并在 onCreate 中重新生成它们,但这对我来说似乎不可行,因为我认为无论如何我们可以将它保存在 BackStack 中。
我在 Android 开发人员 http://developer .android.com/guide/topics/fundamentals/tasks-and-back-stack.html 说明了这些活动属性
android:taskAffinity
android:launchMode
android:allowTaskReparenting
android:clearTaskOnLaunch
android:alwaysRetainTaskState
android:finishOnTaskLaunch
android:singleTask
android:singleInstance
,但我不知道如何使用它们。
有人尝试过这个吗?如果是这样,请帮我把这些碎片拼凑起来。
I have 3 activities A, B and C.
I inflate activity B dynamically on a button click and the user can add as many views as he likes.
The operation is like this:
User sees "Activity A" first enters his details and clicks the save button and I take him to "Activity B" where he adds certain fields as many times he likes and when he clicks save again I take him to "Activity C".
Now when I am at A and go to B and add some views and enter text in TextViews and then hit save and go to C. From C if I hit back I see B intact along with all the inflated views and entered text obviously because it is saved in the Back Stack, but if I go from B to A by hitting back and then come back to B all the views are gone because it is removed from the Back Stack.
I wanted to know if it is possible to hold only one instance of B in the BackStack and not kill it at all when the user hits back?
I had overriden back key but to no avail because the activity is killed anyways, some people suggested that I should save the entire views and data from it to a Parcelable ArrayList and regenerate them again in onCreate but that doesnt look feasible to me as I think we can save it in BackStack anyway.
I came across this guide at android developers http://developer.android.com/guide/topics/fundamentals/tasks-and-back-stack.html which says about these activity attributes
android:taskAffinity
android:launchMode
android:allowTaskReparenting
android:clearTaskOnLaunch
android:alwaysRetainTaskState
android:finishOnTaskLaunch
android:singleTask
android:singleInstance
But I am unaware of how to put them to use.
Has anyone tried this out yet? If so please help me put the pieces together.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,我同意南迪什的观点。
谢谢您,我认为这一点信息会对您有所帮助。
Yes,I agree with Nandeesh.
Thank you I think this little bit information will be helpful for u.
您可以使用它,当不应该关闭活动时返回 false。使用 FLAG_ACTIVITY_REORDER_TO_FRONT 启动上一个 Activity
You could use this, return false when activity should not be closed. use FLAG_ACTIVITY_REORDER_TO_FRONT to launch the previous activity