📜  Android Fragments

📅  最后修改于: 2020-09-30 01:15:52             🧑  作者: Mango

Android Fragments

Android Fragment是活动的一部分,也称为子活动。一个活动中可以有多个片段。片段代表一个活动中的多个屏幕。

Android片段生命周期受活动生命周期的影响,因为片段包含在活动中。

每个片段都有自己的生命周期方法,这些方法受活动生命周期的影响,因为片段嵌入在活动中。

FragmentManager类负责在片段对象之间进行交互。

Android片段生命周期

android片段的生命周期就像活动的生命周期。片段有12种生命周期方法。

Android片段生命周期方法

No.MethodDescription1)onAttach(Activity)it is called only once when it is attached with activity.2)onCreate(Bundle)It is used to initialize the fragment.3)onCreateView(LayoutInflater, ViewGroup, Bundle)creates and returns view hierarchy.4)onActivityCreated(Bundle)It is invoked after the completion of onCreate() method.5)onViewStateRestored(Bundle)It provides information to the fragment that all the saved state of fragment view hierarchy has been restored.6)onStart()makes the fragment visible.7)onResume()makes the fragment interactive.8)onPause()is called when fragment is no longer interactive.9)onStop()is called when fragment is no longer visible.10)onDestroyView()allows the fragment to clean up resources.11)onDestroy()allows the fragment to do final clean up of fragment state.12)onDetach()It is called immediately prior to the fragment no longer being associated with its activity.

Android片段示例

让我们看一下android片段的简单示例。

activity_main.xml




    

    




    
    




    
    


MainActivity类

package example.javatpoint.com.fragmentexample;

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);
    }
}
package example.javatpoint.com.fragmentexample;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment1 extends Fragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_fragment1, container, false);
    }
 }
package example.javatpoint.com.fragmentexample;

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

public class Fragment2 extends Fragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_fragment2, container, false);
    }
    
}

输出: