Android中Fragment、DialogFragment與Activity生命週期釋義
阿新 • • 發佈:2019-02-09
package com.youche.cango.dialog; import android.app.DialogFragment; import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.Window; import com.youche.cango.R; /** * 基類對話方塊 * Created by cango on 2017/1/13.*/ public abstract class BaseDialog extends DialogFragment { private View rootView; private DialogItemLister dialogItemLister; public void setDialogItemLister(DialogItemLister dialogItemLister) { this.dialogItemLister = dialogItemLister; } /** * 1.開始附著在宿主Activity中 * @paramcontext */ @Override public void onAttach(Context context) { super.onAttach(context); } /** * 2.宿主Activity正在建立,它此時在初始化一些物件 * @param savedInstanceState */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } /** * 3.Fargment建立本身的檢視 * @param inflater * @param container * @param savedInstanceState * @return */ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (null != rootView) { ViewGroup parent = (ViewGroup) rootView.getParent(); if (null != parent) { parent.removeView(rootView); } } else { getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE); rootView = inflater.inflate(setLayoutId(), container,false); } return rootView; } /** * 對外介面設定佈局檔案 * * @return */ protected int setLayoutId() { return R.layout.dialog_default_layout; } /** * 4.宿主Activity建立完畢 * @param savedInstanceState */ @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); } /** * 5.宿主Activity準備呈現檢視 */ @Override public void onStart() { super.onStart(); } /** * 6.宿主Activity呈現檢視,自身也呈現出來 */ @Override public void onResume() { super.onResume(); } /** * 7.宿主Activity與自身都進入暫停狀態,從前端進入後臺 */ @Override public void onPause() { super.onPause(); } /** * 8.宿主Activity、Fragment停止渲染,Fragment */ @Override public void onStop() { super.onStop(); } /** * 9.Fragment本身正在銷燬檢視,回收資源 */ @Override public void onDestroyView() { super.onDestroyView(); } /** * 10.宿主Activity銷燬完畢, */ @Override public void onDestroy() { super.onDestroy(); } /** * 11.Fragment與宿主Activity完成離,並完成銷燬操作,回收資源 */ @Override public void onDetach() { super.onDetach(); } /** * 對話方塊,對外回撥介面 */ public interface DialogItemLister{ void onClick(Object obj); } }