package com.fragment; import android.os.Bundle; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.RelativeLayout; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private Button button1; private Button button2; private Button button3; private Button button4; private FragmentManager manager; private RelativeLayout re; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initialize(); button1.setOnClickListener(new View.OnClickListener() { private F1 f1=null; @Override public void onClick(View view) { //模拟Activity栈的方法吧fragment加入栈,如果有该实例就不加人 manager = getSupportFragmentManager(); FragmentTransaction beginTransaction = manager.beginTransaction(); if(f1==null){ f1 = new F1(); beginTransaction.add(R.id.re, f1); beginTransaction.addToBackStack("1"); }else if(f1!=null){ Toast.makeText(MainActivity.this, "第一次", Toast.LENGTH_SHORT).show(); } beginTransaction.commit(); } }); button2.setOnClickListener(new View.OnClickListener() { private F2 f2=null; @Override public void onClick(View view) { //模拟Activity栈的方法吧fragment加入栈,如果有该实例就不加人 manager = getSupportFragmentManager(); FragmentTransaction beginTransaction = manager.beginTransaction(); if(f2==null){ f2 = new F2(); beginTransaction.add(R.id.re, f2); beginTransaction.addToBackStack("2"); } beginTransaction.commit(); } }); button3.setOnClickListener(new View.OnClickListener() { private F3 f3=null; @Override public void onClick(View view) { //模拟Activity栈的方法吧fragment加入栈,如果有该实例就不加人 manager = getSupportFragmentManager(); FragmentTransaction beginTransaction = manager.beginTransaction(); if(f3==null){ f3=new F3(); beginTransaction.add(R.id.re, f3); beginTransaction.addToBackStack("3"); } beginTransaction.commit(); } }); button4.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int num = getSupportFragmentManager().getBackStackEntryCount();//判断你所取得fragment所在栈的位置 Toast.makeText(MainActivity.this, "取出f1" + num, Toast.LENGTH_SHORT).show(); getSupportFragmentManager().popBackStackImmediate("1", 0); } }); } private void initialize() { button1 = (Button) findViewById(R.id.button); button2 = (Button) findViewById(R.id.button2); button3 = (Button) findViewById(R.id.button3); button4 = (Button) findViewById(R.id.button4); re = (RelativeLayout) findViewById(R.id.re); } } //activity_main
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.fragment.MainActivity"> <LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="100dp" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:orientation="horizontal"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1" android:id="@+id/button" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2" android:id="@+id/button2" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="3" android:id="@+id/button3" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="取" android:id="@+id/button4" /> </LinearLayout> <RelativeLayout android:id="@+id/re" android:background="@color/colorAccent" android:layout_width="match_parent" android:layout_height="300dp" android:layout_below="@+id/linearLayout1" android:layout_centerHorizontal="true" android:layout_marginTop="172dp"></RelativeLayout> </RelativeLayout>
fragment回退栈
最新推荐文章于 2023-06-30 14:52:50 发布