【问题标题】:Android. Fragment stays in activity after screen rotation issue安卓。屏幕旋转问题后,片段保持活动状态
【发布时间】:2017-01-12 04:50:12
【问题描述】:

我有两个 activity_main.xml 变体(用于纵向/横向)。在此活动中,用户可以选择项目并浏览有关所选项目的详细信息。

在portraint方向,Fragment动态添加到flFragmentContainer,item选择后,details fragment替换list fragment:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <FrameLayout
        android:id="@+id/flFragmentContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></FrameLayout>
</LinearLayout>

在横向,XML文件中静态描述的片段:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
        <fragment
            android:id="@+id/frgTaskList"
            android:name="com.exprod.xchecklist.fragments.TaskListFragment"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            tools:layout="@layout/fragment_task_list" />
        <fragment
            android:id="@+id/frgTaskDetails"
            android:name="com.exprod.xchecklist.fragments.TaskDetailsFragment"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            tools:layout="@layout/fragment_task_details">
        </fragment>
    </LinearLayout>
</LinearLayout>

这是(MainActivity的)onCreate()方法的代码。

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FragmentManager fragmentManager = getSupportFragmentManager();
        TaskDetailsFragment taskDetailsFragment = (TaskDetailsFragment)fragmentManager.findFragmentById(R.id.frgTaskDetails);
        isDynamicMode = taskDetailsFragment == null || !taskDetailsFragment.isInLayout();
        if(isDynamicMode){
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            Fragment taskListFragment = new TaskListFragment();
            if (savedInstanceState == null) {
                transaction.add(R.id.flFragmentContainer, taskListFragment, TaskListFragment.FRAGMENT_TAG);
            }else{
                transaction.replace(R.id.flFragmentContainer, taskListFragment, TaskListFragment.FRAGMENT_TAG);
            }
            transaction.commit();            
        }
    }

问题:当我将设备旋转到横向时,我在第一个片段 (TaskListFragment) 中调用了两次创建方法(onCreateView(), onActivityCreated(), ... em>)。这表明旧片段仍处于活动状态并在方向更改时重新创建。 我如何才能最终销毁旧片段?我在网上没有找到答案。

P.S: 对不起我的英语不好...

【问题讨论】:

    标签: android android-layout android-fragments android-lifecycle


    【解决方案1】:

    我看到至少有两种方法可以做你想做的事。

    自己改变方向:

    如this document 中所述,您可以防止自动Activity 销毁和重新创建并自行处理方向更改。在这种情况下,您需要先删除旧的Fragment,然后再将新布局扩展至Activity。

    在 Activity 重新创建后移除旧的 Fragment:

    您还可以添加一些确定方向的逻辑,如here 所述(或使用当前设置isDynamicMode 的启发式算法),并在onCreate() 期间手动删除不必要的Fragments。

    更好的方法:

    虽然可以做你想做的事,但我不明白你为什么要这样做——你在纵向和横向配置中都使用TaskListFragment,因此最合乎逻辑的做法是重用它。除了简化事情之外,它还可以让您在方向更改时保持 Fragment 的状态(我相信,这在您的情况下是可取的)。

    【讨论】:

      【解决方案2】:

      感谢 Vasiliy 的回答。我关注这个link 并添加

      android:configChanges="oritentation"

      到我的清单文件:

      <activity android:name=".MyActivity"
            android:configChanges="orientation"
            android:label="@string/app_name">
      

      屏幕旋转时烦人的行为(未附加到活动的片段)消失了

      【讨论】:

        【解决方案3】:

        只需添加到您的活动中 @Override public void onSaveInstanceState(Bundle outState) {} 因为在作为所有Activity的父级的Android Activity中实现这个方法,保存了Fragment。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-12-20
          • 2016-01-04
          • 1970-01-01
          • 2012-01-15
          相关资源
          最近更新 更多