2013-07-30 70 views
-2

我打開我的第一個活動,它只是第二個活動的加載程序,第二個活動中有按鈕,其中一個打開第三個活動。如果我從第三個活動回來而不是第二個活動只是關閉應用程序。有任何解決這個問題的方法嗎?Android後退按鈕關閉應用程序,而不是前往活動

下面是一些代碼片段:

public void onSegmentStatusButtonClick(View view) { 
    Log.d(TAG, "Segment status button clicked."); 
    Intent intent = new Intent(this, SegmentStatusActivity.class); 
    intent.setFlags(Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP); 
    startActivity(intent); 
} 

    @Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_segment_status); 
    TextView txtView = (TextView) this 
      .findViewById(R.id.segment_status_msg); 
    txtView.setText(new String(getIntent().getByteArrayExtra("data"))); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.segment_status, menu); 
    return true; 
} 

@Override 
public void onStop() { 
    super.onStop(); 
    Log.d(TAG, "Executing onStop()."); 
    finish(); 
} 

@Override 
public void onPause() { 
    super.onPause(); 
} 

@Override 
public void onResume() { 
    super.onResume(); 
} 

@Override 
public void onStart() { 
    super.onStart(); 
} 
+1

你爲什麼在onStop()中調用finish()? – dymmeh

+0

http://developer.android.com/reference/android/app/Activity.html#onBackPressed%28%29 –

回答

6

你在你的onStop()調用finish()。我懷疑這是原因。 finish()將表明您已完成活動並隨後會觸發onDestroy()呼叫並將其從背後堆棧中移除。刪除這些調用應該會導致後退導航行爲符合您的期望。只有在您不打算返回時才應完成活動。

+0

謝謝,我相當新的Android開發,並不知道何時正確調用該方法。謝謝。 –

+1

沒問題! http://developer.android.com是一個很好的參考網站,它有官方的api文檔和大量有用的文章和教程來幫助你。當我學習時,這是非常寶貴的。只要堅持下去,你就會學到很多東西! – MattDavis

相關問題