1

I am trying to restart the same Activity after a specific time say after 2 min when a button is clicked. However, it does close the activity, however does not launch in the specified time , here is the code:

public void snoozeup(View view)
{

    Handler mHandler = new Handler();
    mHandler.postDelayed(new Runnable() {

        @Override
        public void run()
        {
            //start your activity here
            startActivity(new Intent(Time_Date.this, Time_Date.class));
        }

    }, a);      //where a is integer with value 120000

    mp.stop();
    mp.release();
    voicePlayer.stop();
    voicePlayer.release();
    songPlayer.stop();
    songPlayer.release();

    this.finish();
}
OBX
  • 6,044
  • 7
  • 33
  • 77

2 Answers2

1

You can't do it in that way - once your Activity is finished, all UI threads are stopped. Your Runnable will never be called.

If you want some functionality to run when your Activity is closed, you need to create a Service.

You should also take note of the problems with using postDelayed as described in postDelayed() in a Service.

Community
  • 1
  • 1
adelphus
  • 10,116
  • 5
  • 36
  • 46
  • but the activity restarts after 2 seconds after finishing it, what would be causing this then? – OBX Aug 22 '15 at 16:13
0

I fixed it by using:

public void snoozeup(View view)
{

    Handler handler = new Handler();
    Runnable x=new Runnable() {
        @Override
        public void run()
        {
            startActivity(new Intent(Time_Date.this, Time_Date.class));
        }
    };
    handler.postDelayed(x, 6000);

    mp.stop();
    mp.release();
    voicePlayer.stop();
    voicePlayer.release();
    songPlayer.stop();
    songPlayer.release();



    finish();
}
OBX
  • 6,044
  • 7
  • 33
  • 77