簡體   English   中英

當按下后退按鈕時,我們如何回溯活動? 即從堆棧彈出活動

[英]How can we backstack activities when back button is pressed ? i.e Pop up activities from the stack

簡單地講,當活動'B'堆疊在活動'A'之后時。想要活動'A'在活動'B'中按下后退按鈕的同時恢復。 不想讓活動“ A”使用intent重新啟動,想要恢復活動“ A”。

您可以使用各種啟動模式標志來影響此行為。 請參閱有關此主題的官方文檔:

https://developer.android.com/guide/components/activities/tasks-and-back-stack

您可以重寫onSaveInstanceState(Bundle savedInstanceState)並編寫要保存為Bundle參數的應用程序狀態值,如下所示:

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
  super.onSaveInstanceState(savedInstanceState);
  // Save UI state changes to the savedInstanceState.
  // This bundle will be passed to onCreate if the process is
  // killed and restarted.
  savedInstanceState.putBoolean("X", true);
  savedInstanceState.putString("Y", "Sultan");
  // etc.
}

該捆綁包將傳遞給onCreate()以及onRestoreInstanceState(),您將在其中提取以下值:

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
  super.onRestoreInstanceState(savedInstanceState);
  // Restore UI state from the savedInstanceState.
  // This bundle has also been passed to onCreate.

  int x = savedInstanceState.getInt("X");
  String y = savedInstanceState.getString("Y");
}

暫無
暫無

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

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