簡體   English   中英

我如何完全調度Activity,所以onCreate將再次被調用

[英]How can i dispatch of Activity completely, so onCreate will be called again

當活動被銷毀時,我想釋放其所有內存,因此當用戶下次進行此活動時,將重新創建所有內容,並再次調用onCreate。 我該怎么做?

它將適用於API版本19及更高版本。

@Override 
protected void onDestroy() {
super.onDestroy();
 // clear App data
((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE)).clearApplicationUserData();
 // restart activity here
 activityrestart();
}

public void activityrestart()
{
Intent i = getBaseContext().getPackageManager()
         .getLaunchIntentForPackage( getBaseContext().getPackageName() );
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}

為您的活動創建一個新的意圖,並在啟動此新實例后調用完成。

Intent intent = new Intent(this, MyActivity.class); startActivity(intent); finish();

在當前活動上調用finish()以使其經歷正常的關閉過程。

暫無
暫無

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

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