• Android开发之Fragment的介绍、使用及生命周期


    Fragment官网介绍-http://developer.android.com/guide/components/fragments.html

    郭大神的使用实例文章:http://blog.csdn.net/sinyu890807/article/details/13171191

    1.Fragment简介及两个版本介绍

    Fragment是Android3.0(API 11)引入的。

    Fragment和Activity的关系(引用官网原话):You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities

    Fragment分为android.app.Fragment(framework version)和android.support.v4.app.Fragment(support version)。

    假如App的最小API是11,那么最好使用android.app.Fragment。

    假如App为了兼容2.3等2.0的Android版本,那么就使用android.support.v4.app.Fragment。

    不过现在的2.3市场占用率已经很低了,Android的API 22都发布了,新开发的app完全可以使用android.app.Fragment。

    使用android.app.Fragment和android.support.v4.app.Fragment在开发中有一些不同,官方原话:

    The main differences when using this support version instead of the framework version are:

    • Your activity must extend FragmentActivity
    • You must call getSupportFragmentManager() to get the FragmentManager

    意思是使用android.support.v4.app.Fragment的时候,1.activity必须继承FragmentActivity  2.在获取FragmentManager的时候,使用getSupportFragmentManager()方法。

    容易出现错误的地方:一个app中不同的Fragment使用不同的Fragment版本,会造成使用一些方法的时候,报错

    The method replace(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, AnotherFragment)

    把引入的Fragment版本修改成一样的就不会报错了。

    2.Fragment的使用

    在Activity中显示Fragment、替换Fragment以及实现返回到上一个Fragment

    a.在MainActivity中显示PlaceholderFragment

     1 import android.app.Activity;
     2 import android.os.Bundle;
     3 
     4 public class MainActivity extends Activity {
     5 
     6     @Override
     7     protected void onCreate(Bundle savedInstanceState) {
     8         super.onCreate(savedInstanceState);
     9         setContentView(R.layout.activity_main);
    10         if (savedInstanceState == null) {
    11             getFragmentManager().beginTransaction()
    12                     .add(R.id.container, new PlaceholderFragment()).commit();
    13         }
    14     }
    15 }

    b.MainActivity中使用AnotherFragment替换PlaceholderFragment,通过点击按钮实现Fragment的替换显示。

     1 public class PlaceholderFragment extends Fragment {
     2 
     3     public PlaceholderFragment() {
     4     }
     5 
     6     @Override
     7     public View onCreateView(LayoutInflater inflater, ViewGroup container,
     8             Bundle savedInstanceState) {
     9         View rootView = inflater.inflate(R.layout.fragment_main, container,false);
    10         rootView.findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
    11             
    12             @Override
    13             public void onClick(View v) {
    14                 // TODO Auto-generated method stub
    15                 getFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.container, new AnotherFragment()).commit();
    //addToBackStack(tag)方法,把这个Fragment加入到backStack中,实现返回。
    16 } 17 }); 18 return rootView; 19 } 20 }

    c.从AnotherFragment返回到PlaceholderFragment,通过点击按钮实现;同时在PlaceholderFragment中的addToBackStack()方法,实现了按Back键返回操作。

     1 public class AnotherFragment extends Fragment {
     2     
     3     @Override
     4     public View onCreateView(LayoutInflater inflater,
     5             @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
     6         // TODO Auto-generated method stub
     7         View root=inflater.inflate(R.layout.anotherfragment, container,false);
     8         
     9         root.findViewById(R.id.btnReturnFragment).setOnClickListener(new OnClickListener() {
    10             
    11             @Override
    12             public void onClick(View v) {
    13                 // TODO Auto-generated method stub
    14                 getFragmentManager().popBackStack();   //当前Fragment弹出BackStack,显示上一个Fragment。
    15             }
    16         });
    17         return root;
    18     }
    19 
    20 
    21 }

    3.Fragment的生命周期

    引用官网的生命周期图

     在开发过程中,一般实现onCreate(),onCreateView(),onPause()方法就可以满足需求了。

  • 相关阅读:
    nginx article
    学习历程
    GRPC
    学习记录
    JAVA知识点记录
    flag
    读的文章
    This usually happens because your environment has changed since running `npm install`.
    expandedRowKeys、expandedRowsChange、expand
    服务端高并发分布式架构演进之路
  • 原文地址:https://www.cnblogs.com/liyiran/p/4642515.html
Copyright © 2020-2023  润新知