简体   繁体   English

Android:片段会导致方向改变的问题

[英]Android: Fragments causing problems on orientation change

According to the Google example here I developed an app based on fragments. 根据此处的Google示例我开发了基于片段的应用程序。

My main activity contains a listfragment of titles and, if it is created in landscape mode, a details fragment. 我的主要活动包含标题的列表片段,如果以横向模式创建,则包含详细信息片段。 If the app is startet in portrait mode, the main activity contains only the listfragment and, if a list item is clicked, start a new activity which shows the detailsfragment. 如果应用程序以纵向模式启动,则主活动仅包含listfragment,如果单击列表项,则启动一个显示detailsfragment的新活动。

If I stay in either the portrait or landscape mode, everything works fine. 如果我保持人像或风景模式,一切正常。 But as soon as I change the orientation multiple problems occur. 但是,一旦我改变方向,就会出现许多问题。

1st problem: starting in portrait mode, then changing to landscape mode, the activity is added to the activity stack twice and I have to press the back button twice to close my app. 第一个问题:从纵向模式开始,然后更改为横向模式,将该活动两次添加到活动堆栈中,我必须按两次返回按钮以关闭我的应用程序。 I cant image this is the way Google wants this to work, so how do I avoid this? 我无法想象这是Google希望其工作的方式,那么如何避免这种情况呢?

2nd problem: when changing from landscape mode to portait mode, the list is shown and not the detailsfragment with the currently selected item. 第二个问题:当从横向模式更改为肖像模式时,将显示列表,而不显示当前所选项目的detailsfragment。 Therefore, all the user input in my detailsfragment is lost. 因此,我的细分中的所有用户输入都丢失了。 This is just annoying and I don't know how to handle this. 这很烦人,我不知道该如何处理。 Do I have to care about the orientation change programmatically in every activity? 我是否必须在每项活动中以编程方式关心方向的变化?

3rd problem: When I switch between n details in landscape mode, as soon as I change to portrait mode, I have to press the back button n times to close my add as the fragments are in the back stack (although they aren't visible any more). 第三个问题:当我在横向模式下在n个细节之间切换时,一旦切换到纵向模式,我必须按后退按钮n次以关闭我的添加,因为片段在后堆栈中(尽管它们不可见还有)。 Do I have to clean the back stack myself in orientation change? 在方向改变时,我是否必须自己清洁后部堆栈?

There is one thing about Activities. 活动是一回事。 That is, when you change the orientation, the Activity restarts unless you do the following: 也就是说,当您更改方向时,活动将重新启动,除非您执行以下操作:

-First, add this in your manifest (inside the activity tag), so you will be able to tell the application what to do in case you change the orientation: -首先,将其添加到清单中(在活动标记内),这样您就可以告诉应用程序在更改方向时该怎么做:

android:configChanges="orientation"

-Second, implement the following method in case you need to to something in case the orientation changes. 第二,在方向发生变化的情况下需要采取以下措施时,请实施以下方法。 If not, with the one before the user won't loose its data. 如果没有,则用户不会丢失之前的数据。

onOrientationChanged (int orientation)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM