在我们的平时的开发当中,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