我们都知道给一个activity动态添加fragment的时候 有下面几种添加方式
看一下布局文件
<LinearLayout 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"
android:orientation="vertical"
> <FrameLayout
android:layout_width="wrap_content"
android:layout_height="100dp"
android:id="@+id/show_fragment"
></FrameLayout> </LinearLayout>
效果图:
一个帧布局动态显示Fragment对象,
1、
package com.xqx_backfragment; import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class MainActivity extends Activity { private Button btn_new;
private Button btn_back;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_new = (Button) findViewById(R.id.newfragment);
btn_back = (Button) findViewById(R.id.backfragment);
//新建一个新的fragment
btn_new.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
//创建管理者对象
FragmentManager manager = getFragmentManager();
//创建事务对象
FragmentTransaction action = manager.beginTransaction();
MyFragment fragment = new MyFragment();
action.add(R.id.show_fragment, fragment);
action.commit(); // MyFragment fragment = new MyFragment();
// action.add(R.id.show_fragment, fragment);
// action.addToBackStack(null);
// action.commit();
}
}); //返回
btn_back.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
onBackPressed();
}
});
} }
add
//创建管理者对象
FragmentManager manager = getFragmentManager();
//创建事务对象
FragmentTransaction action = manager.beginTransaction();
MyFragment fragment = new MyFragment();
action.add(R.id.show_fragment, fragment);
action.commit();
每次点击新建按钮,都执行上面一段代码,也就是新建一个Fragment对象并加入到事务中,原来的fragment效果被覆盖,只显示最后加入事务的fragment对象,但是此时存在多个fragment对象,之前加入事务的fragment对象都一直存在
点击返回按钮销毁activity 退出activity
2、
package com.xqx_backfragment; import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class MainActivity extends Activity { private Button btn_new;
private Button btn_back;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_new = (Button) findViewById(R.id.newfragment);
btn_back = (Button) findViewById(R.id.backfragment);
//新建一个新的fragment
btn_new.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
//创建管理者对象
FragmentManager manager = getFragmentManager();
//创建事务对象
FragmentTransaction action = manager.beginTransaction(); action.replace(R.id.show_fragment, fragment);
action.commit(); }
}); //返回
btn_back.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
onBackPressed();
}
});
} }
replace
//创建管理者对象
FragmentManager manager = getFragmentManager();
//创建事务对象
FragmentTransaction action = manager.beginTransaction();
MyFragment fragment = new MyFragment();
action.replace(R.id.show_fragment, fragment);
action.commit();
每次点击新建按钮,都执行上面一段代码,也就是新建一个Fragment对象并加入到事务中,并且替代原来存在的fragment对象,这种情况下R.id.show_fragment视图上始终只有一个fragment对象
点击返回按钮销毁activity 退出activity
3、
package com.xqx_backfragment; import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class MainActivity extends Activity { private Button btn_new;
private Button btn_back;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_new = (Button) findViewById(R.id.newfragment);
btn_back = (Button) findViewById(R.id.backfragment);
//新建一个新的fragment
btn_new.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
//创建管理者对象
FragmentManager manager = getFragmentManager();
//创建事务对象
FragmentTransaction action = manager.beginTransaction(); MyFragment fragment = new MyFragment();
action.add(R.id.show_fragment, fragment);
action.addToBackStack(null);
action.commit(); }
}); //返回
btn_back.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
onBackPressed();
}
});
} }
addToBackStack
//创建管理者对象
FragmentManager manager = getFragmentManager();
//创建事务对象
FragmentTransaction action = manager.beginTransaction(); MyFragment fragment = new MyFragment();
action.add(R.id.show_fragment, fragment);
action.addToBackStack(null);
action.commit();
每次点击新建按钮,都执行上面一段代码,也就是新建一个Fragment对象并加入到事务中,并将对象加入到回退栈中显示出新的fragment效果图
点击返回按钮显示当前fragment之前的fragment效果图 直到没有fragment对象时activity销毁退出
----------------------------------------------------------------------------------------------------------------------------------------
根据需求选择上述三种事务添加fragment对象的方法。
注意:
1、每次添加fragment对象都要将事务提交才有效果(action.commit()) ,下面情况没有事务提交 则fragment对象没有成功加入事务
//创建管理者对象
FragmentManager manager = getFragmentManager();
//创建事务对象
FragmentTransaction action = manager.beginTransaction();
MyFragment fragment = new MyFragment();
action.add(R.id.show_fragment, fragment);
正确写法:
//创建管理者对象
FragmentManager manager = getFragmentManager();
//创建事务对象
FragmentTransaction action = manager.beginTransaction();
MyFragment fragment = new MyFragment();
action.add(R.id.show_fragment, fragment);
7 action.commit();
2、每次事务提交之前都要有这个事务对象,下面情况action.commit()之后 不能继续action.commit()
//创建管理者对象
FragmentManager manager = getFragmentManager();
//创建事务对象
FragmentTransaction action = manager.beginTransaction();
MyFragment fragment = new MyFragment();
action.add(R.id.show_fragment, fragment);
action.commit();
MyFragment fragment1 = new MyFragment();
action.add(R.id.show_fragment, fragment1);
action.commit();
正确写法:
//创建管理者对象
FragmentManager manager = getFragmentManager();
//创建事务对象
FragmentTransaction action = manager.beginTransaction();
MyFragment fragment = new MyFragment();
action.add(R.id.show_fragment, fragment);
action.addToBackStack(null);
action.commit(); action = manager.beginTransaction();
MyFragment fragment1 = new MyFragment();
action.add(R.id.show_fragment, fragment1);
action.addToBackStack(null);
action.commit();