在我的應用程序中,我正在使用菜單後退按鈕返回到上一個活動。我的一個活動正在從其之前的活動中獲取意圖,並在完成http調用之後發送意向到下一個活動,以便流程類型爲A1 - 意圖 - > A2 - 意圖 - > A3。問題是當我從A3返回到活動A2時,它崩潰了。我相信原因是空的。所以我說下面的代碼在活動A3和A2移動應用程序崩潰時,我使用菜單後退按鈕
活動A3
public boolean onOptionsItemSelected(MenuItem menuItem) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (menuItem.getItemId()) {
case android.R.id.home:
Intent homeIntent = new Intent(context, AllClasses.class);
homeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
homeIntent.putExtras(b); // bundle b has the required data for A2
startActivity(homeIntent);
}
return super.onOptionsItemSelected(menuItem);
}
活動A2
protected void onReStart(){
// retreiving data from intent
Bundle b = new Bundle();
b = getIntent().getExtras();
String imessage_string = b.getString("imessage");
Log.d("onrestart",imessage_string);
}
問題: - 現在的問題是,現在如果我按後退按鈕的菜單欄上A3首先我看到消息「不幸的項目已經停止」,然後按「確定」後,A2開始像我已經從A1移動到A2,並打電話給asyncTask。如果我評論「homeIntent.putExtras(b);」在A3然後我收到錯誤消息兩次,直接跳到A1。我相信在返回A2時,我的onRestart()方法沒有被調用,因爲我沒有對asyncTask進行任何調用。有沒有人知道我應該在這裏做什麼?感謝您提前幫助.... :)
你想要使用onResume(),你必須重寫,而不是onReStart()。另外,如果操作欄處理進入家庭活動,你不應該明確地啓動onOptionsItemsSelected() – Martin 2014-12-01 19:19:04
好吧...我的座右銘是去前一個屏幕,如果我不使用onOptionsltemsSelected()那麼怎麼做我將id傳回給需要調用asyncTask方法的A2。 – 2014-12-01 19:27:42
根據這裏描述的活動生命週期http://developer.android.com/training/basics/activity-lifecycle/pausing.html我應該使用onReStart(),因爲當我打電話給另一個活動我的上一個活動停止(隱藏) ...你不同意嗎? – 2014-12-01 19:45:07