activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btn_add_frag1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ADD Fragment1" />
<Button
android:id="@+id/btn_add_frag2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ADD Fragment2" />
<Button
android:id="@+id/btn_remove_frag2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Remove Fragment2" />
<Button
android:id="@+id/btn_repalce_frag1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="replace Fragment1" />
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
fragment1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff00f0"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is fragment 1"
android:textColor="#000000"
android:textSize="25sp" />
</LinearLayout>
fragment2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffff00"
android:orientation="vertical" >
<TextView
android:id="@+id/fragment2_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is fragment 2"
android:textColor="#000000"
android:textSize="25sp" />
</LinearLayout>
Fragment1
package com.example.harvicblog3_1;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import java.util.List;
public class Fragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment1, container, false);
}
}
Fragment2
package com.example.harvicblog3_1;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class Fragment2 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.fragment2, container, false);
}
}
MainActivity
package com.example.harvicblog3_1;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.Button;
import android.view.View;
public class MainActivity extends FragmentActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnAddFrag1 = (Button) findViewById(R.id.btn_add_frag1);
Button btnAddFrag2 = (Button) findViewById(R.id.btn_add_frag2);
Button btnRemoveFrage2 = (Button) findViewById(R.id.btn_remove_frag2);
Button btnReplaceFrage1 = (Button) findViewById(R.id.btn_repalce_frag1);
btnAddFrag1.setOnClickListener(this);
btnAddFrag2.setOnClickListener(this);
btnRemoveFrage2.setOnClickListener(this);
btnReplaceFrage1.setOnClickListener(this);
}
@Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.btn_add_frag1: {
Fragment1 fragment1 = new Fragment1();
addFragment(fragment1, "fragment1");
}
break;
case R.id.btn_add_frag2: {
Fragment2 fragment2 = new Fragment2();
addFragment(fragment2, "fragment2");
}
break;
case R.id.btn_remove_frag2: {
removeFragment2();
}
break;
case R.id.btn_repalce_frag1: {
replaceFragment1();
}
}
}
private void addFragment(Fragment fragment, String tag) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.fragment_container, fragment, tag);
transaction.commit();
}
private void removeFragment2() {
FragmentManager manager = getSupportFragmentManager();
Fragment fragment = manager.findFragmentByTag("fragment2");
FragmentTransaction transaction = manager.beginTransaction();
transaction.remove(fragment);
transaction.commit();
}
private void replaceFragment1() {
FragmentManager manager = getSupportFragmentManager();
Fragment2 fragment2 = new Fragment2();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.fragment_container, fragment2);
transaction.commit();
}
}
**************************************************************操作二*************************************
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_add_frag1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD Fragment1"
android:layout_weight="1" />
<Button
android:id="@+id/btn_add_frag2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD Fragment2"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_add_frag3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD Fragment3" />
<Button
android:id="@+id/btn_add_frag4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD Fragment4" />
</LinearLayout>
<Button
android:id="@+id/btn_pop_back_stack"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="popBackStack" />
<Button
android:id="@+id/btn_back_to_frg2_0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="BackToFrag2_0" />
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
fragment1.xml fragment2.xml fragment3.xml fragment4.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff00f0"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is fragment 1"
android:textColor="#000000"
android:textSize="25sp" />
</LinearLayout>
Fragment1 Fragment2 Fragment3 Fragment4
package com.example.harvicblog3_2;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment1, container, false);
}
}
package com.example.harvicblog3_2;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentManager.OnBackStackChangedListener;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends FragmentActivity implements
View.OnClickListener {
private int stackID1, stackID2, stackID3, stackID4;
private OnBackStackChangedListener listener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnAddFrag1 = (Button) findViewById(R.id.btn_add_frag1);
Button btnAddFrag2 = (Button) findViewById(R.id.btn_add_frag2);
Button btnAddFrag3 = (Button) findViewById(R.id.btn_add_frag3);
Button btnAddFrag4 = (Button) findViewById(R.id.btn_add_frag4);
Button btnPopBackStack = (Button) findViewById(R.id.btn_pop_back_stack);
Button btnBackToFrag2_0 = (Button) findViewById(R.id.btn_back_to_frg2_0);
btnAddFrag1.setOnClickListener(this);
btnAddFrag2.setOnClickListener(this);
btnAddFrag3.setOnClickListener(this);
btnAddFrag4.setOnClickListener(this);
btnPopBackStack.setOnClickListener(this);
btnBackToFrag2_0.setOnClickListener(this);
//Fragment管理器、添加回退监听
FragmentManager manager = getSupportFragmentManager();
listener = new FragmentManager.OnBackStackChangedListener() {
@Override
public void onBackStackChanged() {
Log.d("qijian", "backstack changed");
}
};
manager.addOnBackStackChangedListener(listener);
}
//移除监听
@Override
protected void onDestroy() {
super.onDestroy();
FragmentManager manager = getSupportFragmentManager();
manager.removeOnBackStackChangedListener(listener);
}
@Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.btn_add_frag1: {
Fragment1 fragment1 = new Fragment1();
stackID1 = addFragment(fragment1, "fragment1");
}
break;
case R.id.btn_add_frag2: {
Fragment2 fragment2 = new Fragment2();
stackID2 = addFragment(fragment2, "fragment2");
}
break;
case R.id.btn_add_frag3: {
Fragment3 fragment3 = new Fragment3();
stackID3 = addFragment(fragment3, "fragment3");
}
break;
case R.id.btn_add_frag4: {
Fragment4 fragment4 = new Fragment4();
stackID4 = addFragment(fragment4, "fragment4");
}
break;
//类似于back键,回退
case R.id.btn_pop_back_stack: {
popBackStack();
}
break;
case R.id.btn_back_to_frg2_0: {
popBackStackToFrag2_0();
}
break;
}
}
private int addFragment(Fragment fragment, String stackName) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.fragment_container, fragment);
transaction.addToBackStack(stackName);
return transaction.commit();
}
private void popBackStack() {
FragmentManager manager = getSupportFragmentManager();
manager.popBackStack();
}
private void popBackStackToFrag2_0() {
FragmentManager manager = getSupportFragmentManager();
manager.popBackStack("fragment2", 0);// 方法一,通过TAG回退
// manager.popBackStack(stackID2, 0);//方法二,通过Transaction ID回退
}
}
*******************************************操作三*******************************************
activity_main.xml
<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"
tools:context=".MainActivity">
<Button
android:id="@+id/btn_add_frag1"
android:text="ADD Frag1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_add_other_frags"
android:text="ADD Frag2,3,4"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_popBackStack"
android:text="popBackStack"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"></FrameLayout>
</LinearLayout>
fragment1.xml fragment2.xml fragment3.xml fragment4.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff00f0"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is fragment 1"
android:textColor="#000000"
android:textSize="25sp" />
</LinearLayout>
Fragment1 Fragment2 Fragment3 Fragment4
package com.example.harvicblog3_2;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment1, container, false);
}
}
MainActivity
package com.example.harvicblog3_3;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.Button;
public class MainActivity extends FragmentActivity implements
View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnAddFrag1 = (Button) findViewById(R.id.btn_add_frag1);
Button btnAddOtherFrags = (Button) findViewById(R.id.btn_add_other_frags);
Button btnPopStack = (Button) findViewById(R.id.btn_popBackStack);
btnAddFrag1.setOnClickListener(this);
btnAddOtherFrags.setOnClickListener(this);
btnPopStack.setOnClickListener(this);
}
@Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.btn_add_frag1: {
addFragment1();
}
break;
case R.id.btn_add_other_frags: {
addOtherFragments();
}
break;
case R.id.btn_popBackStack: {
popBackStack();
}
break;
}
}
private void addFragment1() {
Fragment1 fragment1 = new Fragment1();
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.fragment_container, fragment1);
transaction.addToBackStack("fragment1");
transaction.commit();
}
/**
* 、首先,添加Fragment1,提交一次事务 2、然后,一次添加Fragment2,Fragment3,Fragment4,然后提交一次事务
* 3、利用popBackStack
* ()将顶层事务出栈,可以看到把Fragment2,Fragment3,Fragment4一次出栈,界面显示在了Fragment1的位置
* ,这就充分说明了,回滚是以提交的事务为单位进行的!
*/
private void addOtherFragments() {
Fragment2 fragment2 = new Fragment2();
Fragment3 fragment3 = new Fragment3();
Fragment4 fragment4 = new Fragment4();
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.fragment_container, fragment2);
transaction.add(R.id.fragment_container, fragment3);
transaction.add(R.id.fragment_container, fragment4);
transaction.addToBackStack("other fragments");
transaction.commit();
}
private void popBackStack() {
FragmentManager manager = getSupportFragmentManager();
manager.popBackStack();
}
}