[英]CountdownTimer: How to restart from a remaining time when the user goes back to the previous activity?
我是 Android 初學者,這可能是非常基本的問題。 我正在嘗試創建一個CountDownTimer
,它在兩個活動之間保持倒計時。 用戶可以轉到 Activity B 並從 Activity B 返回到 Activity A。我正在考慮將剩余時間保存在SharedPreferences
。 我想知道的是,當用戶執行OnBackPressed
並將當前剩余時間保存到SharedPreferences
並返回到上一個活動時,如何從上一個活動的剩余時間重新啟動倒數計時器?
我認為這種方法應該可以幫助您:
@Override
public void onResume() { // This will be trigger when your activity is created or come to front
// Load preference
// Start timer
}
@Override
public void onStop() { // This will be triggered when your activity goes behind or before your activity destroyed.
// Cancel timer
// Save preference
}
其實很簡單,看評論就行。 讓我知道這是否有幫助
為了得到這個,你必須有一個單獨的線程來計算倒計時的東西,它會在你在活動 1 或其他活動中的所有天氣中運行。 並且該線程應該更新活動(檢查活動是否可見)。 更新活動可以有多種方式。
希望這會幫助你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.