0

I want to change from my current activity to another while I am inside a Handler. The idea is other code inside the handler will run until a certain condition doesnt match (with increment of the count value every time). When the count value matches the condition I close the activity and move to another.

my code is:

  mHandler = new Handler();

        mRunnable = new Runnable() {
            @Override
            public void run() {

if(count<CONDITION_VALUE)
{
//do other stuff...


    count++;

}else
{



   //change activity...
    finish();

}


                mHandler.postDelayed(mRunnable, 4000);

            }
        };
        mHandler.postDelayed(mRunnable, (1000));

The code is running without any error but the old activity is not being destroyed (i guess) and the new activity is reloaded after every 4 seconds.

I want the new activity to load only once. How can I achieve this?

Zubayr
  • 457
  • 4
  • 18
  • Finish destroys the activity but the handler is in an async thread in an endless loop – ivan Jun 24 '19 at 15:33

1 Answers1

0

try this, from enter link description here

in your Activity

@Override
public void onDestroy() {
   mHandler.removeCallbacks(mRunnable);
   super.onDestroy();
}
Turvy
  • 882
  • 1
  • 8
  • 23