我想动态地将片段添加到布局中。但是在屏幕旋转时,片段会再次添加到上一个片段的顶部。
布局:
<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();
}
}
源代码来自:https://github.com/aporter/coursera-android/tree/master/Examples/FragmentDynamicLayout
答案 0 :(得分:3)
这是因为每次旋转屏幕时,都会重新创建您的活动,即再次调用onCreate()
。由于您在onCreate
中创建了片段,因此每次都会创建一个新片段,而前一个片段不会被销毁。
要解决此问题,您可以使用savedInstanceState
保存活动的状态,也可以保存片段已创建一次的布尔值,然后在onCreate
中检查是否创建片段或不