簡體   English   中英

Android Fragment的視圖在“活動娛樂”中重復

[英]Android Fragment's view is duplicated on Activity Recreation

當一個活動產生一個片段並隨后重新創建(例如,通過旋轉屏幕)時,與片段相關聯的視圖將被復制,而當片段后來被銷毀時,只有一個被銷毀。

當且僅當該活動直接在其onSaveInstanceState的覆蓋中調用super.onSaveInstanceState或僅不覆蓋該回調時,才發生這種情況。

要復制的最小代碼:MainActivity.java:

package com.example.trevor.test;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.CheckBox;
import android.widget.CompoundButton;

/**
 * Created by trevor on 11/11/16.
 */

public class MainActivity extends Activity {
    MainFragment fragment = new MainFragment();
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        CheckBox checkbox = (CheckBox)findViewById(R.id.checkBox);
        checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(b)
                {
                    getFragmentManager().beginTransaction().add(R.id.container,fragment).commit();
                }
                else
                {
                    getFragmentManager().beginTransaction().remove(fragment).commit();
                }
            }
        });
    }
}

MainFragment.java:

package com.example.trevor.test;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by trevor on 11/11/16.
 */

public class MainFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        return inflater.inflate(R.layout.fragment_main,container,false);
    }
}

activity_main.xml:

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

    <CheckBox
        android:text="CheckBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/checkBox" />

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/container">

    </FrameLayout>
</LinearLayout>

fragment_main.xml:

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

    <TextView
        android:text="Open"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView" />
</LinearLayout>

AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.trevor.test">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>

    </application>

</manifest>

預期的行為:選中該框會使單詞“ open”出現在下面。 取消選中會導致單詞消失。

實際行為:選中此框會使單詞“ open”出現在下面。 如果隨后旋轉屏幕,則“打開”一詞會變暗,並且取消選中該框將使該詞變成其常規陰影。

重新創建Activity時,將自動還原最初添加的Fragment 這是Fragment的標准行為。 此外,在Activity重新創建后,將恢復CheckBox的選中狀態,因此其onCheckedChanged()方法將再次觸發,並加載Fragment另一個實例。 如果要在選中CheckBox情況下繼續更改方向,則越來越多的Fragment實例將不斷堆積。 您需要在添加實例之前檢查Fragment實例是否已經存在。

由於Fragment將自動重新添加,因此在OnCheckedChangeListener添加和刪​​除它會很麻煩,因為您首先需要檢查它是否已連接到FragmentManager ,然后確定它是否顯示。 在確保實例化和添加之后,僅根據需要hide()show()可能會更簡單。

例如:

fragment = (MainFragment) getFragmentManager().findFragmentById(R.id.container);

checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        if(b)
        {
            if(fragment == null) {
                fragment = new MainFragment();
                getFragmentManager().beginTransaction().add(R.id.container, fragment).commit();
            }
            else {
                getFragmentManager().beginTransaction().show(fragment).commit();
            }
        }
        else
        {
            if (fragment != null) {
                getFragmentManager().beginTransaction().hide(fragment).commit();
            }
        }
    }
});

然后,您可以從MainFragment的聲明中刪除初始化。

MainFragment fragment;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM