Pergunta

I'm launching an activity from a broadcast receiver using the Intent.FLAG_ACTIVITY_NEW_TASK flag.

My activity launches a CountDownTimer in its onCreate method. Pressing a key kills my app (I'm calling finish()) but my app appears in the task mgr.

If I'll open the task mgr and click on my app, the onCreate method is being called again and another instance of my timer is being called.

How can I prevent this behavior?

Thanks, Avi

Foi útil?

Solução

You might have something like the code below in your app to start your countdown timer.

new CountDownTimer(30000, 1000) {

 public void onTick(long millisUntilFinished) {
     mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
 }

 public void onFinish() {
     mTextField.setText("done!");
 }
}.start();

You could wrap an if statement around that to see if the countdown has started or not, if it has then set a boolean to true and if not set it to false (since the timer has stopped).

    if(countDownStarted == false)
    {
        new CountDownTimer(30000, 1000) 
        {
            public void onTick(long millisUntilFinished) 
            {
                mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
                countDownStarted = true;
            }

            public void onFinish() 
            {
                mTextField.setText("done!");
                countDownStarted = false;
            }
        }.start();
    }

Outras dicas

In your Manifest-file put launchMode in the <activity> tag.

If you choose singleTask or singleInstance you won't have multiple instances of your app.

example:

<activity android:name=".MyFirstActivity" android:launchMode="singleTask" />

Try using that, it might solve your problem. More information can be found here

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top