fragment与回退栈back stack

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一致


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值