android - 使用 fragment 改变方向 - 错误的 Activity 开始

标签 android layout android-layout android-activity android-fragments

我有一个使用 fragment 的应用程序。根据屏幕尺寸和方向,我显示不同的布局:
1。 layout-fragment - 选择列表
2。 layout-fragment - display selected choice
我在处理大屏幕方向变化时遇到了问题。在纵向模式下,我在一个布局中显示两个 fragment ,但在横向模式下,如果用户选择显示某事,我只显示第二个 fragment Activity 。当在此状态下方向更改回纵向模式时,我想再次显示两个 fragment ,但我再次显示第二个布局 fragment Activity 。
我知道这是 android Activity 管理,但最好的方法是什么它按我希望的方式工作(始终以纵向模式显示 1 和 2 fragment )?

感谢您的任何建议!

最佳答案

这是因为您的第二个 Activity (仅显示一个 fragment )是在方向更改时重新创建的。

您在这里可以做的是在您的 onCreate 中检查方向,如果是纵向 - 以适当的 Intent 启动 Activity_1,以便它可以启动第二个 fragment 以获得正确的细节。当然,您还必须编辑 Activity_1 以在 onCreate 中检查此 Intent 。

一些示例代码,其中 intent 具有操作 ACTION_VIEW 和详细信息对象的 uri。

Activity_2(一个 fragment - 只能用于横向)

// in method onCreate
// assume orientation is checked and boolean set
if (isPortraitOrientation) {
   startActivity(suitableIntent); // ACTION_VIEW with uri
   finish();
   return;
} else {
   // load fragment
}

Activity_1(如果方向允许则显示两个 fragment

// check intent
// assume orientation checked and boolean set  
final Intent intent = getIntent();
final String action = intent.getAction();
Log.v(TAG, " - action: " + action);
if (isPortraitOrientation && Intent.ACTION_VIEW.equals(action)) {
   // load fragment with selected choice
}

推荐的方向检查: Check orientation on Android phone

关于android - 使用 fragment 改变方向 - 错误的 Activity 开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6741754/

相关文章:

android - 如何将按钮添加到抽屉导航菜单?

android - 自定义TabLayout中选定的选项卡

android - 方向更改默认时间延迟...?

android - 芯片组边距和多线

android - 在 Android 应用程序中记住密码

java - 错误 : Execution failed for task ':app:dexDebug' . > com.android.ide.common.process.ProcessException : org. gradle.process.internal.ExecException

android - Fabric Crashlytics 报告显示一些字母而不是我的 Android 应用程序的类名称

css - 表格显示的表格标签不好......但是调查问题怎么样?

android 动态壁纸 水波纹效果

html - CSS:显示:内联 block 未正确对齐 div