Fragment间的替换和后退

一、新建项目

二、创建第一个Fragment

1.layout文件

<?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">

    <Button
        android:id="@+id/btn_show_another_fragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="显示另一个Fragment" />

</LinearLayout>

fragment_main

2.class文件

package com.example.fragmentdemo;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

/**
 * Created by 袁磊 on 2017/2/7.
 * Fragment生命周期
 */
public class PlaceHolderFragment extends Fragment {
    private static final String TAG = "PlaceHolderFragment";

    public PlaceHolderFragment() {
    }

    private Button btnShowAnotherFragment;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, " a onCreate");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        Log.d(TAG, "a  onCreateView");
        btnShowAnotherFragment = (Button) rootView.findViewById(R.id.btn_show_another_fragment);
        btnShowAnotherFragment.setOnClickListener(myOnClickListener);
        return rootView;
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.d(TAG, "a  onPause");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.d(TAG, "a  onDestroyView");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "a  onDestroy");
    }

    private View.OnClickListener myOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btn_show_another_fragment:
                    getFragmentManager().beginTransaction()
                            .addToBackStack(null)//添加到后退栈,为了支持后退按钮
                            .replace(R.id.container, new AnotherFragment())//替换
                            .commit();//提交
                    break;
            }

        }
    };
}

PlaceHolderFragment

三、创建第二个Fragment

1.layout文件

<?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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="这是另一个Fragment" />

    <Button
        android:id="@+id/btn_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="后退" />

</LinearLayout>

fragment_another

2.class文件

package com.example.fragmentdemo;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by 袁磊 on 2017/2/7.
 * Fragment常用生命周期
 */
public class AnotherFragment extends Fragment {

    private static final String TAG = "AnotherFragment";

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_another, container, false);
        Log.d(TAG, "onCreateView");
        root.findViewById(R.id.btn_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getFragmentManager().popBackStack();//后退
            }
        });
        return root;
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.d(TAG, "onPause");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.d(TAG, "onDestroyView");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy");
    }
}

AnotherFragment

四、使用

1.activity_main中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>

activity_main

2.MainActivity中

package com.example.fragmentdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceHolderFragment())//添加
                    .commit();//提交
        }
    }
}

MainActivity

五、运行效果

   

时间: 2024-08-23 23:39:29

Fragment间的替换和后退的相关文章

Android学习路线(二十三)运用Fragment构建动态UI——Fragment间通讯

先占个位置,下次翻译 :p In order to reuse the Fragment UI components, you should build each as a completely self-contained, modular component that defines its own layout and behavior. Once you have defined these reusable Fragments, you can associate them with

Fragment间的通信

在网上看到的一篇文章,总结的很好 为了重用Fragment的UI组件,创建的每个Fragment都应该是自包含的.有它自己的布局和行为的模块化组件.一旦你定义了这些可重用的Fragment,你就可以把它们跟一个Activity关联,并把它们跟应用程序的逻辑相连来实现全部的组合式UI. 现实中我们经常想要一个Fragment跟另一个Fragment进行通信,例如,要基于一个用户事件来改变内容.所有的Fragment间的通信都是通过跟关联的Activity来完成的.另个Fragment不应该直接通信

Fragment间的交互

两个单独的Fragment是 不应该进行通信的,应该用他们所在的activity作为 通信的纽带. 为了实现 两个Fragment的交互,我们在Fragment中定义一个接口,然后在中定义 一个方法,在 Fragment的onAttach()方法中调用这个接口中的方法,然后让Activity实现这个方法,来完成 Activity和Fragment之间的通信 . public class HttpFragment extends Fragment { public static HttpFragm

Fragment加载替换add,show,hide,replace方法

使用replace方法把原有的Fragment替换掉: 使用hide和show方法,把已经添加过的Fragment再次显示出来. 使用replace(R.id.layout,B)即可以.但是这带来一个问题,原来A会在被replace后被销毁,会调用其生命周期函数(onDestoryView(),onPause(),onDestory()). 如果频繁地replace Fragment会不断创建新实例,销毁旧的,无法重用. 多次切换,会导致Fragment上的View无法加载的问题(onCreat

Fragment 创建及替换

1.Fragment的产生与介绍 Android运行在各种各样的设备中,有小屏幕的手机,超大屏的平板甚至电视.针对屏幕尺寸的差距,很多情况下,都是先针对手机开发一套App,然后拷贝一份,修改布局以适应平板神马超级大屏的.难道无法做到一个App可以同时适应手机和平板么,当然了,必须有啊.Fragment的出现就是为了解决这样的问题.你可以把Fragment当成Activity的一个界面的一个组成部分,甚至Activity的界面可以完全有不同的Fragment组成,更帅气的是Fragment拥有自己

Android Fragment间的广播消息接收

这种方式不用在配置文件加东西,我比较喜欢. 广播注册,可以写在Activity(onCreate),也可以写在Fragment(onActivityCreated)里. LocalBroadcastManager broadcastManager = LocalBroadcastManager.getInstance(getActivity()); IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction("an

Android之Fragment用法

本文翻译自Android developer网站上面,原文参考:Building a Dynamic UI with Fragments 当我们需要动态的多界面切换的时候,就需要将UI元素和Activity融合成一个模块.在2.3中我们一般通过各种Activity中进行跳转来实现多界面的跳转和单个界面动态改变.在4.0或以上系统中就可以使用新的特性来方便的达到这个效果--Fragment类.Fragment类似一个嵌套Activity,可以定义自己的layout和自己的生命周期. 多个Fragm

Android初级Fragment用法

  当我们需要动态的多界面切换的时候,就需要将UI元素和Activity融合成一个模块.在2.3中我们一般通过各种Activity中进行跳转来实现多界面的跳转和单个界面动态改变.在4.0或以上系统中就可以使用新的特性来方便的达到这个效果--Fragment类.Fragment类似一个嵌套Activity,可以定义自己的layout和自己的生命周期. 多个Fragment可以放在一个Activity中(所以上面讲到类似一个嵌套Activity),而这个类可以对这些Fragment进行配置以适应不同

Fragment使用

当我们需要动态的多界面切换的时候,就需要将UI元素和Activity融合成一个模块.在2.3中我们一般通过各种Activity中进行跳转来实现多界面的跳转和单个界面动态改变.在4.0或以上系统中就可以使用新的特性来方便的达到这个效果--Fragment类.Fragment类似一个嵌套Activity,可以定义自己的layout和自己的生命周期. ? ? ??? 多个Fragment可以放在一个Activity中(所以上面讲到类似一个嵌套Activity),而这个类可以对这些Fragment进行配