fragment与回退栈back stack
作用:因在同一ViewGroup中显示多个Fragment,因此需要回退到上一次的Fragment时,需要回退栈
FragmentTransaction.addToBackStack(String name) 将当前的Fragment增加到回退栈中
实例
逻辑代码--MainActivity
package com.example.week3_day2_fragment_backstack;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener {
private Button btn1;
private Button btn2;
private Button btn3;
private Button btn4;
private FragmentManager manager;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
btn3 = (Button) findViewById(R.id.btn3);
btn4 = (Button) findViewById(R.id.btn4);
// 注册监听器
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
btn4.setOnClickListener(this);
manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.ld, new Fragment1());
transaction.commit();
}
// 实现监听器中的方法
@SuppressLint("NewApi")
public void onClick(View v) {
FragmentTransaction transaction = manager.beginTransaction();
switch (v.getId()) {
case R.id.btn1:
transaction.replace(R.id.ld, new Fragment1());
break;
case R.id.btn2:
transaction.replace(R.id.ld, new Fragment2());
break;
case R.id.btn3:
transaction.replace(R.id.ld, new Fragment3());
break;
case R.id.btn4:
transaction.replace(R.id.ld, new Fragment4());
break;
default:
break;
}
//将Fragment保存到回退栈中 参数Tag 标记 如果没有的话 null
transaction.addToBackStack(null);
transaction.commit();
}
}
逻辑代码--fragment1
package com.example.week3_day2_fragment_backstack;
import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@SuppressLint("NewApi")
public class Fragment1 extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment1, container,false);
return view;
}
}
fragment2,fragment3,fragment4与fragment1一致,分别加载fragment2,fragment3,fragment4
布局文件--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="horizontal" >
<LinearLayout
android:id="@+id/ld1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="青春" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="校园" />
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="旅行" />
<Button
android:id="@+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="音乐" />
</LinearLayout>
<LinearLayout
android:id="@+id/ld"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
布局文件--fragment1
<?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:orientation="vertical" >
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="青春" />
</LinearLayout>
布局文件-
fragment2,fragment3,fragment4与fragment1一致