1、Fragment的回退栈
将当前事务添加到回退栈:transaction.addToBackStack(null);
不考虑回退栈的情况下,FragmentA用replace跳转到FragmentB,FragmentA实例会被销毁,此时按back键,Activity退出。
添加当前事务到回退栈后,FragmentA实例不会被销毁,但视图层次依然会被销毁,此时按back键,回到FragmentA,再按back键,Activity退出。
视图重绘(replace):
OneFragment oneFragment = new OneFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.containerFragment,oneFragment,"ONE");
transaction.addToBackStack(null);
transaction.commit();
视图不重绘(hide+add):
TwoFragment twoFragment = new TwoFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.hide(this);
transaction.add(R.id.containerFragment,twoFragment,"TWO");
transaction.addToBackStack(null);
transaction.commit();
public class FragmentDemoActivity extends AppCompatActivity implements TwoFragment.FTwoBtnClickListener{
private FrameLayout containerFragment;
private OneFragment oneFragment;
private TwoFragment twoFragment;
DialogFragment
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment_demo);
containerFragment = (FrameLayout)findViewById(R.id.containerFragment);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
// if(dm.widthPixels > dm.heightPixels){
// oneFragment = new OneFragment();
// // transaction.add(R.id.containerFragment,oneFragment,"ONE");
// //用add在屏幕旋转的时候会有fragment重叠问题,改成replace后没有重叠问题了
// transaction.replace(R.id.containerFragment,oneFragment,"ONE");
// }else{
// twoFragment = new TwoFragment();
// transaction.replace(R.id.containerFragment,twoFragment,"TWO");
// }
//第二种解决屏幕旋转fragment重叠问题:
if(savedInstanceState == null) {
if (dm.widthPixels > dm.heightPixels) {//横屏显示的Fragment
oneFragment = new OneFragment();
transaction.add(R.id.containerFragment, oneFragment, "ONE");
} else { //竖屏显示的Fragment
twoFragment = new TwoFragment();
transaction.add(R.id.containerFragment, twoFragment, "TWO");
}
transaction.commit();
}
}
//Activity管理Fragment之间的互操作,不要在一个Fragment里操作另一个Fragment,降低耦合性
@Override
public void onFTwoBtnClick() {
if(oneFragment == null){
oneFragment = new OneFragment();
oneFragment.setFOneBtnClickListener(new OneFragment.FOneBtnClickListener() {
@Override
public void onFOneBtnClick() {
if(twoFragment == null){
twoFragment = new TwoFragment();
}
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// transaction.hide(oneFragment);
// transaction.add(R.id.containerFragment,twoFragment,"TWO");
transaction.replace(R.id.containerFragment,twoFragment,"TWO");
transaction.addToBackStack(null);
transaction.commit();
}
});
}
FragmentTransaction transaction = getSupportFragmentManager().beginTra
将当前事务添加到回退栈:transaction.addToBackStack(null);
不考虑回退栈的情况下,FragmentA用replace跳转到FragmentB,FragmentA实例会被销毁,此时按back键,Activity退出。
添加当前事务到回退栈后,FragmentA实例不会被销毁,但视图层次依然会被销毁,此时按back键,回到FragmentA,再按back键,Activity退出。
视图重绘(replace):
OneFragment oneFragment = new OneFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.containerFragment,oneFragment,"ONE");
transaction.addToBackStack(null);
transaction.commit();
视图不重绘(hide+add):
TwoFragment twoFragment = new TwoFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.hide(this);
transaction.add(R.id.containerFragment,twoFragment,"TWO");
transaction.addToBackStack(null);
transaction.commit();
public class FragmentDemoActivity extends AppCompatActivity implements TwoFragment.FTwoBtnClickListener{
private FrameLayout containerFragment;
private OneFragment oneFragment;
private TwoFragment twoFragment;
DialogFragment
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment_demo);
containerFragment = (FrameLayout)findViewById(R.id.containerFragment);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
// if(dm.widthPixels > dm.heightPixels){
// oneFragment = new OneFragment();
// // transaction.add(R.id.containerFragment,oneFragment,"ONE");
// //用add在屏幕旋转的时候会有fragment重叠问题,改成replace后没有重叠问题了
// transaction.replace(R.id.containerFragment,oneFragment,"ONE");
// }else{
// twoFragment = new TwoFragment();
// transaction.replace(R.id.containerFragment,twoFragment,"TWO");
// }
//第二种解决屏幕旋转fragment重叠问题:
if(savedInstanceState == null) {
if (dm.widthPixels > dm.heightPixels) {//横屏显示的Fragment
oneFragment = new OneFragment();
transaction.add(R.id.containerFragment, oneFragment, "ONE");
} else { //竖屏显示的Fragment
twoFragment = new TwoFragment();
transaction.add(R.id.containerFragment, twoFragment, "TWO");
}
transaction.commit();
}
}
//Activity管理Fragment之间的互操作,不要在一个Fragment里操作另一个Fragment,降低耦合性
@Override
public void onFTwoBtnClick() {
if(oneFragment == null){
oneFragment = new OneFragment();
oneFragment.setFOneBtnClickListener(new OneFragment.FOneBtnClickListener() {
@Override
public void onFOneBtnClick() {
if(twoFragment == null){
twoFragment = new TwoFragment();
}
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// transaction.hide(oneFragment);
// transaction.add(R.id.containerFragment,twoFragment,"TWO");
transaction.replace(R.id.containerFragment,twoFragment,"TWO");
transaction.addToBackStack(null);
transaction.commit();
}
});
}
FragmentTransaction transaction = getSupportFragmentManager().beginTra