简体   繁体   中英

Android creates extra fragments after holder activity destroyed by orientation change

I have an acticity that has simple FrameLayout as its layout.I add a fragment with textview programatically.When I change orientation,Android destroys fragment and its holder activity as expected but in next recreation, Android creates an extra fragment.This is increasing incrementally.For example after 3rd orientation change, Android creates 4 new fragments and I have only one new Fragment() statement in my code.Anyways, I prepared a minimum length code that reproduces this behavior.

FragmentHolderActivity.java

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class FragmentHolderActivity extends Activity {

    private final String TAG = "FragmentHolderActivity";

    private Fragment mFragment;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment_holder);

        mFragment = new TestFragment();

        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        fragmentTransaction.add(R.id.fragment_holder,mFragment);
        fragmentTransaction.commit();
    }

}

TestFragment.java

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

public class TestFragment extends Fragment {

    private final String TAG = "TestFragment";

    public TestFragment() {
// Required empty public constructor
    }

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(TAG,": Entered onCreate() for " + this.toString());
    }

@Override
    public void onDestroy(){
        super.onDestroy();
        Log.i(TAG,": Entered onDestroy() for " + this.toString());
    }

@Override
    public void onDetach(){
        super.onDetach();
        Log.i(TAG, ": Entered onDetach() for " + this.toString());
    }

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
// Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_test, container, false);
    }

}

activity_fragment_holder.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/fragment_holder"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

fragment_text.xml

<FrameLayout 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"
    tools:context="com.example.cagri.fragmentcrashtest.TestFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />

</FrameLayout>

The problem lies with the "new TestFragment()" part in the onCreate method.

Try it this way:

FragmentManager fragmentManager = getFragmentManager();
mFragment = (TestFragment) fragmentManager.findFragmentByTag(FRAGMENT_TAG);

// If not-null, it is currently being kept as the configuration changes
if (mFragment == null) {
      mFragment = new TestFragment();
      fragmentManager .beginTransaction().add(mFragment, FRAGMENT_TAG).commit();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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