有关回滚——FragmentTransaction

有关回滚——FragmentTransaction
第一步:建立4个fragment.xml,分别叫做fragment1.xml,fragment2.xml,fragment3.xml,fragment4.xml

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="#00ff00"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/textview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第一个页面"/>


</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="#ff00ff"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/textview2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第2个页面"/>


</LinearLayout>

fragment3.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="#00ffff"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/textview3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第3个页面"/>


</LinearLayout>

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="#ff0000"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/textview4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第4个页面"/>


</LinearLayout>

第二步:
建立4个Fragment.java 文件分别命名为Fragment1,Fragment2,Fragment3,Fragment4,
Fragment1

package com.example.fragment3;

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);
    }

}

2,3,4相同
第三步:修改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"
    >

    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button 
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="添加第一个"
            />
         <Button 
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="添加第2个"
            />
    </LinearLayout>
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button 
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="添加第3个"
            />
         <Button 
            android:id="@+id/button4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="添加第4个"
            />
    </LinearLayout>
     <Button 
            android:id="@+id/button5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="popBackStack"
            />
     <Button 
            android:id="@+id/button6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Backto2.0"
            />
      <Button 
            android:id="@+id/button7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Backto_inclusive"
            />
      <FrameLayout 
          android:id="@+id/frame"
           android:layout_width="match_parent"
            android:layout_height="match_parent"
          ></FrameLayout>

</LinearLayout>

第四步:写main.class

package com.example.fragment3;

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.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends FragmentActivity {
    private int stackID1,stackID2,stackID3,stackID4;



    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button1=(Button) findViewById(R.id.button1);
        Button button2=(Button) findViewById(R.id.button2);
        Button button3=(Button) findViewById(R.id.button3);
        Button button4=(Button) findViewById(R.id.button4);
        Button button5=(Button) findViewById(R.id.button5);
        Button button6=(Button) findViewById(R.id.button6);
        Button button7=(Button) findViewById(R.id.button7);
        FragmentManager manager=getSupportFragmentManager();
        manager.addOnBackStackChangedListener(new OnBackStackChangedListener(){

            @Override
            public void onBackStackChanged() {
                // TODO Auto-generated method stub
                Toast.makeText(MainActivity.this, "add", Toast.LENGTH_SHORT).show();
            }

        });
        button1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Fragment1 fragment1=new Fragment1();
                //stackID1=
                addFragment(fragment1,"fragment1");

            }
        });
button4.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Fragment fragment4=new Fragment4();
                //stackID4=
                        addFragment(fragment4,"fragment4");

            }
        });
button2.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        Fragment2 fragment2=new Fragment2();
        //stackID2=
                addFragment(fragment2,"fragment2");

    }
});
button3.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        Fragment3 fragment3=new Fragment3();
        //stackID3=
                addFragment(fragment3,"fragment3");

    }
});
      button5.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            FragmentManager manager=getSupportFragmentManager();
            manager.popBackStack();
        }
    });
      button6.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            FragmentManager manager=getSupportFragmentManager();
            manager.popBackStack("fragment2",0);
            //manager.popBackStack(stackID2,0);
            //manager.p
        }
    });
      button7.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            FragmentManager manager=getSupportFragmentManager();
            manager.popBackStack("fragment2", FragmentManager.POP_BACK_STACK_INCLUSIVE);
        }
    });
    }
    private void addFragment(Fragment fragment,String TAG){
        FragmentManager manager=getSupportFragmentManager();
        FragmentTransaction transaction=manager.beginTransaction();  
        transaction.add(R.id.frame,fragment );
        transaction.addToBackStack(TAG);
        //return 
                transaction.commit();
    }
    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        FragmentManager manager=getSupportFragmentManager();
        manager.removeOnBackStackChangedListener(new OnBackStackChangedListener(){

            @Override
            public void onBackStackChanged() {
                // TODO Auto-generated method stub
                //manager.removeOnBackStackChangedListener(arg0)
                Toast.makeText(MainActivity.this, "remove", Toast.LENGTH_SHORT).show();
            }

        });
        super.onDestroy();
    }


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值