1

以下のコードは、support lib Fragmentが使用されている場合、またはネイティブフラグメント(互換性libではない)が使用されている場合、動作が異なります。support libを使用して、以下のコードはBFragmentを破棄します。ネイティブフラグメントを使用すると、NFragmentは破棄されます。

なぜこの違いがあるのか​​誰かが知っていますか?

public class MainActivity extends Activity {


    class BFragment extends Fragment {

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            final View inflated = inflater.inflate(R.layout.dummy, container, false);
             ((TextView) inflated.findViewById(R.id.txt_dummy)).setText("BFragment");
            return inflated;
        }

    }

    class NFragment extends Fragment {

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            final View inflated = inflater.inflate(R.layout.dummy, container, false);
             ((TextView) inflated.findViewById(R.id.txt_dummy)).setText("NFragment");
            return inflated;
        }

    }

    static NFragment nfragment;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        nfragment = new NFragment();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        FragmentManager fm = getFragmentManager();
        fm.beginTransaction().replace(R.id.fragment_container, new BFragment()).addToBackStack(null).commit();
        fm.beginTransaction().replace(R.id.notification_container, nfragment).commit();

        fm.beginTransaction().replace(R.id.fragment_container, new BFragment()).addToBackStack(null).commit();
        fm.beginTransaction().replace(R.id.notification_container, nfragment).commit();

        return true;

    }
}

activity_main.xmlは次のとおりです。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >


    <FrameLayout
       android:id="@+id/notification_container"
       android:layout_width="match_parent"
       android:layout_height="wrap_content" 
       android:layout_alignParentLeft="true"
       android:layout_alignParentTop="true"
       />


    <FrameLayout 
       android:id="@+id/fragment_container"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:layout_marginTop="100dp"
       android:layout_alignParentLeft="true"
       android:layout_alignParentTop="true"
       />


</RelativeLayout>

そしてこれはdummy.xml(フラグメントによって使用される)です:

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

<TextView
    android:id="@+id/txt_dummy"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>
4

0 に答える 0