質問

I am new to Android . As per the Android Developers Doc making an activity launchmode singleTop it will keep that activity intact . But its not working for me .I have an Activity where i have a countdown timer , what i want is when i leave that Activity on back press and return to that Activity that countdown timer should still be running . How to do it ? Please Help

役に立ちましたか?

解決

I believe you misunderstood a bit.

Launching an activity in singleTop does not mean that the activity is "intact", it means that if the target task already has an existing instance of the activity at the top of its stack, that instance will receive the new intent - a new instance won't be created. (This is opposite to launching activities in standard mode, which every time there's a new intent a new instance of the class is created to respond to that intent.)

As others suggested, you could bind to a Service and update the countdown time from there.

他のヒント

"Creating a Service will always keep that timer running , instead i want it to keep running only when application is alive", that means you want to continue the timer from where the user left.
Store the timer value in SharedPreferences onStop() and retrieve the same onRestart() and then finally continue updating

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top