Android中关于Fragment的使用和Fragment的生命周期

在我们的平时的开发当中,Fragment的使用时越来越广泛了。现在我们就来使用一下Fragment。当然,在布局中使用Fragment,包括静态的使用和动态的使用。现在我们就来学习一下如何动态使用Fragment。架构图如下:content_main中的内容如下://一个空的布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/fragment"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="startservicefromanotherapp.lg.com.app.MainActivity"
    tools:showIn="@layout/activity_main">

</RelativeLayout>
//fragment_main中的布局内容如下:
<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"
    tools:context="learnfragment.lg.com.learnfragment.MainActivityFragment"
    tools:showIn="@layout/activity_main">

    <Button
        android:id="@+id/btnStartAnotherFragment"
        android:text="启动另外一个Fragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>



//another_fragment.xml中的内容如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:text="这是另一个Fragment的内容"
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <Button
        android:text="启动第一个fragment界面"
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

//MainActivity中的内容如下:
package learnfragment.lg.com.learnfragment;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
/*需要注意的是在MainActivity中调用的是getSupportFragmentManager()
* 这和继承了Fragment的类调用的是getFragmentManager()
* 这俩个方法是不一样的,注意区分*/
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /*这里需要提醒的是,如果我们是使用了Fragment布局的话,那么我们的
        * content_main布局中的内容应该是什么都没有的,否则是不行的。
        * 否则会影响界面的效果*/
        setContentView(R.layout.activity_main);
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.fragment, new MainActivityFragment())
                    .commit();
        }

    }
}


//
package learnfragment.lg.com.learnfragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
MainActivityFragment中的内容如下:
 * A placeholder fragment containing a simple view. */public class MainActivityFragment extends Fragment {    public MainActivityFragment() {    }    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        System.out.println("Main-->onCreate");    }    @Override    public void onStart() {        super.onStart();        System.out.println("Main-->onStart");    }    @Override    public void onAttach(Context context) {        super.onAttach(context);        System.out.println("Main-->onAttach");    }    @Override    public void onResume() {        super.onResume();        System.out.println("Main-->onResume");    }    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container,                             Bundle savedInstanceState) {           View rootView=inflater.inflate(R.layout.fragment_main, container, false);        rootView.findViewById(R.id.btnStartAnotherFragment).setOnClickListener(new View.OnClickListener() {            /*如果为了实现按back键的时候,能够返回到第一个fragment的界面,那么就需要            * replace之前调用addToBackStack()的这个方法,否则按back键的时候,这时候            * 程序是会退出的*/            @Override            public void onClick(View v) {                getFragmentManager().beginTransaction().addToBackStack(null).                        replace(R.id.fragment, new AnotherFragment()).commit();            }        });        System.out.println("Main-->onCreateView");        return rootView;    }    @Override    public void onPause() {        super.onPause();        System.out.println("Main-->onPause");    }    @Override    public void onDestroyView() {        super.onDestroyView();        System.out.println("Main-->onDestroyView");    }    @Override    public void onDetach() {        super.onDetach();        System.out.println("Main-->onDetach");    }    @Override    public void onDestroy() {        super.onDestroy();        System.out.println("Main-->onDestroy");    }}


//AnotherFragment中的内容如下:
package learnfragment.lg.com.learnfragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by 李果 on 2016/7/10.
 */
public class AnotherFragment extends android.support.v4.app.Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.another_frgment, container, false);

        /*这个方法可以实现俩个Fragment之间的通信*/
        getActivity().findViewById(R.id.btnStartAnotherFragment);
        root.findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /*方法一*/
               /* getFragmentManager().beginTransaction().replace(R.id.fragment, new MainActivityFragment())
                        .commit();*/
                /*方法二*/
                getFragmentManager().popBackStack();
            }
        });
        return root;
    }
}
//最后的界面如下:
 
我们现在来测试一下Fragment的生命周期:
1.当程序开始的时候:
 Main-->onAttach
 Main-->onCreate
 Main-->onCreateView
 Main-->onStart
 Main-->onResume
2.当我们点击了“启动另一个Fragment”的按钮的时候:
 Main-->onPause
 onDestroyView
 Main-->onDestroyView
3.当进入另外一个Fragment的界面的时候,点击“启动第一个Fragment的界面“的按钮的时候,那么:
 Main-->onCreateView
 Main-->onStart
 Main-->onResume
4.当我们点击back按键的时候,程序退出。
 Main-->onDestroyView
 Main-->onDestroy
 Main-->onDetach

最后附上郭霖大神关于这方面的博客:
http://blog.csdn.net/guolin_blog/article/details/8881711
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱coding的同学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值