Android中Activity和Fragment生命周期執(zhí)行順序

一些基礎(chǔ)忘記了刁愿;寫(xiě)了個(gè)簡(jiǎn)單的demo記錄下谅畅;

簡(jiǎn)單記錄下Activity和Fragment生命周期執(zhí)行順序

Activity和Fragment生命周期圖


截屏2020-05-03 下午3.55.21.png

寫(xiě)了個(gè)demo打印了下:

E/FFFF==>>>OneActivity: onCreate

E/FFFF==>>>FragmentOne: onAttach

E/FFFF==>>>FragmentOne: onCreate

E/FFFF==>>>FragmentOne: onCreateView

E/FFFF==>>>FragmentOne: onActivityCreated

E/FFFF==>>>FragmentOne: onStart

E/FFFF==>>>OneActivity: onStart

E/FFFF==>>>OneActivity: onResume

E/FFFF==>>>FragmentOne: onResume

E/FFFF==>>>FragmentOne: onPause

E/FFFF==>>>OneActivity: onPause

E/FFFF==>>>TwoActivity: onCreate

E/FFFF==>>>FragmentTwo: onAttach

E/FFFF==>>>FragmentTwo: onCreate

E/FFFF==>>>FragmentTwo: onCreateView

E/FFFF==>>>FragmentTwo: onActivityCreated

E/FFFF==>>>FragmentTwo: onStart

E/FFFF==>>>TwoActivity: onStart

E/FFFF==>>>TwoActivity: onResume

E/FFFF==>>>FragmentTwo: onResume

E/FFFF==>>>FragmentOne: onStop

E/FFFF==>>>OneActivity: onStop

E/FFFF==>>>FragmentTwo: onPause

E/FFFF==>>>TwoActivity: onPause

E/FFFF==>>>OneActivity: onRestart

E/FFFF==>>>FragmentOne: onStart

E/FFFF==>>>OneActivity: onStart

E/FFFF==>>>OneActivity: onResume

E/FFFF==>>>FragmentOne: onResume

E/FFFF==>>>FragmentTwo: onStop

E/FFFF==>>>TwoActivity: onStop

E/FFFF==>>>FragmentTwo: onDestroyView

E/FFFF==>>>FragmentTwo: onDestroy

E/FFFF==>>>FragmentTwo: onDetach

E/FFFF==>>>TwoActivity: onDestroy

以下為代碼丰泊,代碼很簡(jiǎn)單~

public class OneActivity extends AppCompatActivity {

private final static String TAG="FFFF==>>>OneActivity";

@Override

protected void onCreate(@Nullable Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_one);

    Log.e(TAG, "onCreate");

    // Create a new Fragment to be placed in the activity layout

    FragmentActiv firstFragment = new FragmentActiv();

    // In case this activity was started with special instructions from an

    // Intent, pass the Intent's extras to the fragment as arguments

// firstFragment.setArguments(getIntent().getExtras());

    // Add the fragment to the 'fragment_container' FrameLayout

    getSupportFragmentManager().beginTransaction()

            .add(R.id.one_1, firstFragment).commit();

}

@Override

protected void onRestart() {

    super.onRestart();

    Log.e(TAG, "onRestart");

}

@Override

protected void onStart() {

    super.onStart();

    Log.e(TAG, "onStart");

}

@Override

protected void onResume() {

    super.onResume();

    Log.e(TAG, "onResume");

}

@Override

protected void onStop() {

    super.onStop();

    Log.e(TAG, "onStop");

}

@Override

protected void onPause() {

    super.onPause();

    Log.e(TAG, "onPause");

}

@Override

protected void onDestroy() {

    super.onDestroy();

    Log.e(TAG, "onDestroy");

}

public void doLoginAction(View view) {

    startActivity(new Intent(OneActivity.this,TwoActivity.class));

}

}

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="[http://schemas.android.com/apk/res-auto](http://schemas.android.com/apk/res-auto)"

xmlns:tools="[http://schemas.android.com/tools](http://schemas.android.com/tools)"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:id="@+id/one_1">

<Button

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="One!"

    app:layout_constraintBottom_toBottomOf="parent"

    app:layout_constraintLeft_toLeftOf="parent"

    app:layout_constraintRight_toRightOf="parent"

    app:layout_constraintTop_toTopOf="parent"

    android:onClick="doLoginAction"/>

</androidx.constraintlayout.widget.ConstraintLayout>

public class TwoActivity extends AppCompatActivity {

private final static String TAG="FFFF==>>>TwoActivity";

@Override

protected void onCreate(@Nullable Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_two);

    Log.e(TAG, "onCreate");

    // Create a new Fragment to be placed in the activity layout

    FragmentBctiv firstFragment = new FragmentBctiv();

    // In case this activity was started with special instructions from an

    // Intent, pass the Intent's extras to the fragment as arguments

// firstFragment.setArguments(getIntent().getExtras());

    // Add the fragment to the 'fragment_container' FrameLayout

    getSupportFragmentManager().beginTransaction()

            .add(R.id.two_2, firstFragment).commit();

}

@Override

protected void onRestart() {

    super.onRestart();

    Log.e(TAG, "onRestart");

}

@Override

protected void onStart() {

    super.onStart();

    Log.e(TAG, "onStart");

}

@Override

protected void onResume() {

    super.onResume();

    Log.e(TAG, "onResume");

}

@Override

protected void onStop() {

    super.onStop();

    Log.e(TAG, "onStop");

}

@Override

protected void onPause() {

    super.onPause();

    Log.e(TAG, "onPause");

}

@Override

protected void onDestroy() {

    super.onDestroy();

    Log.e(TAG, "onDestroy");

}

}

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="[http://schemas.android.com/apk/res-auto](http://schemas.android.com/apk/res-auto)"

xmlns:tools="[http://schemas.android.com/tools](http://schemas.android.com/tools)"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity"

android:id="@+id/two_2">

<Button

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="Two!"

    app:layout_constraintBottom_toBottomOf="parent"

    app:layout_constraintLeft_toLeftOf="parent"

    app:layout_constraintRight_toRightOf="parent"

    app:layout_constraintTop_toTopOf="parent"

    android:onClick="doLoginAction"/>

</androidx.constraintlayout.widget.ConstraintLayout>

public class FragmentOne extends Fragment {

private final static String TAG="FFFF==>>>FragmentOne";

@Override

public void onAttach(@NonNull Context context) {

    super.onAttach(context);

    Log.e(TAG, "onAttach");

// getArguments();

}

@Override

public void onCreate(@Nullable Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    Log.e(TAG, "onCreate");

}

@Nullable

@Override

public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    Log.e(TAG, "onCreateView");

    return super.onCreateView(inflater, container, savedInstanceState);

}

@Override

public void onActivityCreated(@Nullable Bundle savedInstanceState) {

    super.onActivityCreated(savedInstanceState);

    Log.e(TAG, "onActivityCreated");

}

@Override

public void onStart() {

    super.onStart();

    Log.e(TAG, "onStart");

}

@Override

public void onResume() {

    super.onResume();

    Log.e(TAG, "onResume");

}

@Override

public void onPause() {

    super.onPause();

    Log.e(TAG, "onPause");

}

@Override

public void onStop() {

    super.onStop();

    Log.e(TAG, "onStop");

}

@Override

public void onDestroyView() {

    super.onDestroyView();

    Log.e(TAG, "onDestroyView");

}

@Override

public void onDestroy() {

    super.onDestroy();

    Log.e(TAG, "onDestroy");

}

@Override

public void onDetach() {

    super.onDetach();

    Log.e(TAG, "onDetach");

}

}

public class FragmentTwo extends Fragment {

private final static String TAG="FFFF==>>>FragmentTwo";

@Override

public void onAttach(@NonNull Context context) {

    super.onAttach(context);

    Log.e(TAG, "onAttach");

}

@Override

public void onCreate(@Nullable Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    Log.e(TAG, "onCreate");

}

@Nullable

@Override

public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    Log.e(TAG, "onCreateView");

    return super.onCreateView(inflater, container, savedInstanceState);

}

@Override

public void onActivityCreated(@Nullable Bundle savedInstanceState) {

    super.onActivityCreated(savedInstanceState);

    Log.e(TAG, "onActivityCreated");

}

@Override

public void onStart() {

    super.onStart();

    Log.e(TAG, "onStart");

}

@Override

public void onResume() {

    super.onResume();

    Log.e(TAG, "onResume");

}

@Override

public void onPause() {

    super.onPause();

    Log.e(TAG, "onPause");

}

@Override

public void onStop() {

    super.onStop();

    Log.e(TAG, "onStop");

}

@Override

public void onDestroyView() {

    super.onDestroyView();

    Log.e(TAG, "onDestroyView");

}

@Override

public void onDestroy() {

    super.onDestroy();

    Log.e(TAG, "onDestroy");

}

@Override

public void onDetach() {

    super.onDetach();

    Log.e(TAG, "onDetach");

}

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市唾那,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌褪尝,老刑警劉巖闹获,帶你破解...
    沈念sama閱讀 211,194評(píng)論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異河哑,居然都是意外死亡避诽,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,058評(píng)論 2 385
  • 文/潘曉璐 我一進(jìn)店門(mén)璃谨,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)沙庐,“玉大人,你說(shuō)我怎么就攤上這事佳吞」俺” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 156,780評(píng)論 0 346
  • 文/不壞的土叔 我叫張陵底扳,是天一觀(guān)的道長(zhǎng)铸抑。 經(jīng)常有香客問(wèn)我,道長(zhǎng)衷模,這世上最難降的妖魔是什么鹊汛? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 56,388評(píng)論 1 283
  • 正文 為了忘掉前任,我火速辦了婚禮阱冶,結(jié)果婚禮上刁憋,老公的妹妹穿的比我還像新娘。我一直安慰自己木蹬,他們只是感情好职祷,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,430評(píng)論 5 384
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著,像睡著了一般有梆。 火紅的嫁衣襯著肌膚如雪是尖。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 49,764評(píng)論 1 290
  • 那天泥耀,我揣著相機(jī)與錄音饺汹,去河邊找鬼。 笑死痰催,一個(gè)胖子當(dāng)著我的面吹牛兜辞,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播夸溶,決...
    沈念sama閱讀 38,907評(píng)論 3 406
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼逸吵,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了缝裁?” 一聲冷哼從身側(cè)響起扫皱,我...
    開(kāi)封第一講書(shū)人閱讀 37,679評(píng)論 0 266
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎捷绑,沒(méi)想到半個(gè)月后韩脑,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,122評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡粹污,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,459評(píng)論 2 325
  • 正文 我和宋清朗相戀三年段多,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片壮吩。...
    茶點(diǎn)故事閱讀 38,605評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡进苍,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出鸭叙,到底是詐尸還是另有隱情琅捏,我是刑警寧澤,帶...
    沈念sama閱讀 34,270評(píng)論 4 329
  • 正文 年R本政府宣布递雀,位于F島的核電站柄延,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏缀程。R本人自食惡果不足惜搜吧,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,867評(píng)論 3 312
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望杨凑。 院中可真熱鬧滤奈,春花似錦、人聲如沸撩满。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,734評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至昭躺,卻和暖如春忌锯,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背领炫。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 31,961評(píng)論 1 265
  • 我被黑心中介騙來(lái)泰國(guó)打工偶垮, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人帝洪。 一個(gè)月前我還...
    沈念sama閱讀 46,297評(píng)論 2 360
  • 正文 我出身青樓似舵,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親葱峡。 傳聞我的和親對(duì)象是個(gè)殘疾皇子砚哗,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,472評(píng)論 2 348