質問

I want to add fragments dynamically to a layout. but on screen rotation, fragment is added again on top of previous fragment.

Image before any rotation

Image after rotation

layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false"
    android:orientation="horizontal" >

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

    <FrameLayout
        android:id="@+id/quote_fragment_container"
        android:layout_width="0dp"
        android:layout_height="match_parent" />

</LinearLayout>

MainActivity:

package course.examples.Fragments.DynamicLayout;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.util.Log;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import course.examples.Fragments.DynamicLayout.TitlesFragment.ListSelectionListener;

public class QuoteViewerActivity extends Activity implements
ListSelectionListener {

public static String[] mTitleArray;
public static String[] mQuoteArray;

private final QuotesFragment mQuoteFragment = new QuotesFragment();
private FragmentManager mFragmentManager;
private FrameLayout mTitleFrameLayout, mQuotesFrameLayout;

private static final int MATCH_PARENT = LinearLayout.LayoutParams.MATCH_PARENT;
private static final String TAG = "QuoteViewerActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {

Log.i(TAG, getClass().getSimpleName() + ":entered onCreate()");

super.onCreate(savedInstanceState);

mTitleArray = getResources().getStringArray(R.array.Titles);
mQuoteArray = getResources().getStringArray(R.array.Quotes);

setContentView(R.layout.main);

mTitleFrameLayout = (FrameLayout) findViewById(R.id.title_fragment_container);
mQuotesFrameLayout = (FrameLayout) findViewById(R.id.quote_fragment_container);

mFragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = mFragmentManager
.beginTransaction();
fragmentTransaction.add(R.id.title_fragment_container,
new TitlesFragment());
fragmentTransaction.commit();

mFragmentManager
.addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
public void onBackStackChanged() {
setLayout();
}
});
}

private void setLayout() {
if (!mQuoteFragment.isAdded()) {
mTitleFrameLayout.setLayoutParams(new LinearLayout.LayoutParams(
MATCH_PARENT, MATCH_PARENT));
mQuotesFrameLayout.setLayoutParams(new LinearLayout.LayoutParams(0,
MATCH_PARENT));
} else {
mTitleFrameLayout.setLayoutParams(new LinearLayout.LayoutParams(0,
MATCH_PARENT, 1f));
mQuotesFrameLayout.setLayoutParams(new LinearLayout.LayoutParams(0,
MATCH_PARENT, 2f));
}
}

@Override
public void onListSelection(int index) {
if (!mQuoteFragment.isAdded()) {
FragmentTransaction fragmentTransaction = mFragmentManager
.beginTransaction();
fragmentTransaction.add(R.id.quote_fragment_container,
mQuoteFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
mFragmentManager.executePendingTransactions();
}
if (mQuoteFragment.getShownIndex() != index) {
mQuoteFragment.showIndex(index);
}
}

@Override
protected void onDestroy() {
Log.i(TAG, getClass().getSimpleName() + ":entered onDestroy()");
super.onDestroy();
}

@Override
protected void onPause() {
Log.i(TAG, getClass().getSimpleName() + ":entered onPause()");
super.onPause();
}

@Override
protected void onRestart() {
Log.i(TAG, getClass().getSimpleName() + ":entered onRestart()");
super.onRestart();
}

@Override
protected void onResume() {
Log.i(TAG, getClass().getSimpleName() + ":entered onResume()");
super.onResume();
}

@Override
protected void onStart() {
Log.i(TAG, getClass().getSimpleName() + ":entered onStart()");
super.onStart();
}

@Override
protected void onStop() {
Log.i(TAG, getClass().getSimpleName() + ":entered onStop()");
super.onStop();
}

}

source codes from: https://github.com/aporter/coursera-android/tree/master/Examples/FragmentDynamicLayout

役に立ちましたか?

解決

This is because each time you rotate the screen, your activity is recreated i.e., onCreate() is called again. Since you are creating the fragment in onCreate, a new fragment is created each time and the previous one is not destroyed.

To counter this, you can save the state of the activity using savedInstanceState, maybe save a boolean that the fragment has been created once, and then check it in onCreate whether to create the fragment or not.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top