簡體   English   中英

使用intent重新啟動當前活動

[英]Re-launch current activity using intent

對於我正在開發的應用程序,我想使用intent重新啟動當前活動。 所以我在MainActivity.class中,我想使用以下內容重新啟動MainActivity.class:

Intent intent = new Intent(getApplicationContext(), MainActivity.class);

這會調用onDestroy()但不會重新啟動該活動。 為什么這不起作用?

如果你在一個Activity中: this.recreate();

如果你在片段中: getActivity.recreate();

相關鏈接 :

如何重新啟動Android活動

如何重啟android中的活動?

Android活動重啟

你可以使用:

finish();
startActivity(getIntent());

這將完成當前活動,並以與最初創建活動時收到的相同意圖開始新活動。 這應該有效地重新啟動活動。

編輯:

請參閱Android中的重新加載活動

包括這一個......

startActivity(intent); 

這樣做:

  Intent i=getIntent();//This simply returns the intent in which the current Activity is started
  finish();//This would simply stop the current Activity.
  startActivity(i);//This would start a new Activity.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM