碎片被添加到后面的堆栈。如果FragmentOne以纵向显示,屏幕旋转到横向模式,fragmentTwo将显示。然后,如果再次旋转屏幕,则会以纵向模式重新显示fragmentOne(显示为新片段?)。解决这个问题最有效的方法是什么?
public class MainActivity extends AppCompatActivity implements MasterFragment.OnMasterSelectedListener {
FragmentOne mFragmentOne;
Fragmenttwo mFragmentTwo;
Boolean dualPane;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mFragmentOne = new FragmentOne();
mFragmentTwo = new Fragmenttwo();
MasterFragment masterFragment;
FrameLayout frameLayout = (FrameLayout)findViewById(R.id.frameLayout);
FrameLayout frameLayoutDetail = (FrameLayout)findViewById(R.id.frameLayoutDetail);
if (frameLayout !=null ) {
dualPane= false;
Toast.makeText(getApplicationContext(), "single Pane ",
Toast.LENGTH_LONG).show();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
masterFragment=(MasterFragment)getSupportFragmentManager().findFragmentByTag("MASTER");
if (masterFragment==null) {
masterFragment = new MasterFragment();
fragmentTransaction.replace(R.id.frameLayout, masterFragment,"MASTER");
}
fragmentTransaction.commit();
}else {
if (frameLayoutDetail !=null) {
dualPane = true;
Toast.makeText(getApplicationContext(), "dual Pane ",
Toast.LENGTH_LONG).show();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
masterFragment=(MasterFragment)getSupportFragmentManager().findFragmentById(R.id.frameLayoutMaster);
if (masterFragment==null) {
masterFragment = new MasterFragment();
fragmentTransaction.replace(R.id.frameLayoutMaster, masterFragment);
}
fragmentTransaction.commit();
}
}
}
@Override
public void onItemSelected(int position) {
if (!dualPane) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
switch (position) {
case 0:
Toast toast = Toast.makeText(getApplicationContext(), "you clicked 0", Toast.LENGTH_SHORT);
toast.show();
fragmentTransaction.replace(R.id.frameLayout, mFragmentOne)
.addToBackStack(null);
//.commit();
break;
case 1:
Toast toast1 = Toast.makeText(getApplicationContext(), "you clicked 1", Toast.LENGTH_SHORT);
toast1.show();
fragmentTransaction.replace(R.id.frameLayout, mFragmentTwo)
.addToBackStack(null);
//.commit();
break;
}
fragmentTransaction.commit();
}else {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
switch (position) {
case 0:
Toast.makeText(getApplicationContext(), "you clicked 0", Toast.LENGTH_SHORT).show();
fragmentTransaction.replace(R.id.frameLayoutDetail, mFragmentOne)
.addToBackStack(null);
//.commit();
break;
case 1:
Toast.makeText(getApplicationContext(), "you clicked 1", Toast.LENGTH_SHORT).show();
fragmentTransaction.replace(R.id.frameLayoutDetail, mFragmentTwo)
.addToBackStack(null);
//.commit();
break;
}
fragmentTransaction.commit();
}
}
}
MasterFragment:
public class MasterFragment extends ListFragment {
OnMasterSelectedListener mCallback;
public interface OnMasterSelectedListener {
void onItemSelected(int position);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
String[] numbers = new String[]{"One", "Two"};
ListAdapter numAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, numbers);
setListAdapter(numAdapter);
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
mCallback = (OnMasterSelectedListener) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString()
+ " must implement OnMasterSelectedListener");
}
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
mCallback.onItemSelected(position);
//getListView().setItemChecked(position, true);
}
}
FragmetOne:
public class Fragmenttwo extends Fragment {
public Fragmenttwo() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_two, container, false);
}
}
FragmentTwo:
public class Fragmenttwo extends Fragment {
public Fragmenttwo() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_two, container, false);
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
//setRetainInstance(true);
// Save the current article selection in case we need to recreate the fragment
//outState.putInt(ARG_POSITION, mCurrentPosition);
}
}