2013-11-25 60 views
2

我有兩個活動,AB。我叫BA throught驗證碼:如何在Android中手動暫停活動

Intent myIntent = new Intent(this, myAcitivity.class);   
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
startActivity(myIntent); 

B,我把一個按鈕,通過暫停活動B回到活動A。我試着暫停B,以便它進入後臺並轉到A,但它正在工作。我試圖

一個解決辦法:

moveTaskToBack(true);

B背景相反的,它也將在A背景。

任何解決方案?

+1

你能展示這些解決方案中的任何一種嗎? –

+0

請嘗試從Activity B的onPause()方法轉到Activity A –

+0

@PankajKumar,我提到過我試過'moveTaskToBack(true);' –

回答

1

的Android已經在做這個給你。說你是在活動答:您開始與活動B:

Intent myIntent = new Intent(this, myAcitivity.class); 
startActivity(myIntent); 

的onPause()爲當前活動你去myActivity之前將被調用,其中的onCreate()被調用。現在,如果您按回按鈕,myActivity的onPause()會被調用,並且您將返回到調用onResume()的活動A.請閱讀文檔herehere中的活動生命週期。

要保存活動的狀態,你必須覆蓋的onSaveInstanceState()回調方法:

系統調用該方法,當用戶離開你的活動,併爲其傳遞一個將被保存在Bundle對象你的活動被意外銷燬的事件。如果系統稍後必須重新創建活動實例,它將同一個Bundle對象傳遞給onRestoreInstanceState()和onCreate()方法。

例子:

static final String STATE_SCORE = "playerScore"; 
static final String STATE_LEVEL = "playerLevel"; 

@Override 
public void onSaveInstanceState(Bundle savedInstanceState) { 
    // Save the user's current game state 
    savedInstanceState.putInt(STATE_SCORE, mCurrentScore); 
    savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel); 

    // Always call the superclass so it can save the view hierarchy state 
    super.onSaveInstanceState(savedInstanceState); 
} 

當你的活動是重建,就可以恢復從捆綁的狀態:

public void onRestoreInstanceState(Bundle savedInstanceState) { 
    // Always call the superclass so it can restore the view hierarchy 
    super.onRestoreInstanceState(savedInstanceState); 

    // Restore state members from saved instance 
    mCurrentScore = savedInstanceState.getInt(STATE_SCORE); 
    mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL); 
} 

有一個在文檔更多相關信息,請有很好的閱讀關於保存/恢復您的活動狀態here

4

要覆蓋的後退按鈕的行爲,你可以覆蓋onBackPressed()方法在你Activity當你按下後退按鈕被稱爲:

@Override 
public void onBackPressed() { 
    moveTaskToBack(true); // "Hide" your current Activity 
} 

使用moveTaskToBack(true)您的活動發送到後臺,但難保它將保持「暫停」狀態,如果Android需要內存,它可以殺死它。我不知道爲什麼你想要這種行爲我認爲最好是保存活動狀態,並在你回來或簡單地恢復它時,用你想帶來的新活動啓動另一個意向。

或者,

使用此代碼onBackPressed()

boolean mIsPaused = false; 

final Thread workerThread = new Thread(new Runnable() { 
    @Override 
    public void run() { 
    doA(); 
    checkPause(); 
    doB(); 
    checkPause(); 
    ... 
} 
} 
}); 

private void checkPause() { 
while(isPaused()) { 
    // you could also use the notify/wait pattern but that is probably needless  complexity for this use case. 
    Thread.sleep(50); 
} 
} 

private synchronized boolean isPaused() { 
return mIsPaused; 
} 

private synchronized void setPaused(boolean isPaused) { 
mIsPaused = isPaused; 
} 


pauseButton.setOnLongClickListener(new View.OnLongClickListener() { 
@Override 
public boolean onLongClick(View v) { 
    // disable any UI elements that need it 
    setIsPaused(true); 
} 
}); 

unPauseButton.setOnLongClickListener(new View.OnLongClickListener() { 
    @Override 
public boolean onLongClick(View v) { 
    // re-enable any UI elements that need it 
    setIsPaused(false); 
} 
});