activity的startActivity和context的startActivity有什麼不同嗎?

時間 2021-06-02 05:35:05

1樓:brucevanfdm

activity的startActivity()方法可使用預設配置的launch flag,而context的startActivity()須包含FLAG_ACTIVITY_NEW_TASK的launch flag,原因是該context可能沒有現存的任務棧供新建的activity使用,必須顯式指定生成乙個自己單獨的任務棧。

下面是API Reference:

activity.startActivity():

Launch a new activity. You will not receive any information about when the activity exits. This implementation overrides the base version, providing information about the activity performing the launch.

Because of this additional information, the FLAG_ACTIVITY_NEW_TASK launch flag is not required; if not specified, the new activity will be added to the task of the caller.

This method throws ActivityNotFoundException if there was no Activity found to run the given Intent.

context.startActivity()

Launch a new activity. You will not receive any information about when the activity exits.

Note that if this method is being called from outside of an Activity Context, then the Intent must include the FLAG_ACTIVITY_NEW_TASK launch flag. This is because, without being started from an existing Activity, there is no existing task in which to place the new activity and thus it needs to be placed in its own separate task.

This method throws ActivityNotFoundException if there was no Activity found to run the given Intent.

android mvp中activity作為presenter的可行性?

感覺並沒有太大意義,這樣做乙個很大的弊端就是,當你想換個 Presenter 的時候,或者有多個 Presenter 的時候,這個設計就出現了極大問題,不靈活了,失去了 MVP 架構的初衷。 楊麼麼 做sdk可能有用,接入應用給與sdk的activity可能也就乙個,但做應用可能侷限性很大,應用出入...

Android的Activity什麼時候會呼叫onCreate 而不呼叫onStart ?

didikee mInstrumentation.callActivityOnCreate activity,r.state 函式中會判斷 if r.activity.mFinished 執行完 onCreate 後,判斷這時 activity 有沒有finish 沒有就會接著執行 onStart ...

當 Activity 以全屏模式執行時,如何允許 Android 系統狀態列在頂層出現,而不迫使 Activity 重新布局讓出空間?

喬麥 View view findViewById R.id.view view.setSystemUiVisibility View.SYSTEM UI FLAG LAYOUT FULLSCREEN setSystemUiVisibility 的api level為11 View.SYSTEM U...