0

i have a while loop that i need to delay but i cant. i have already tried two method but neither seem to work. here's what i tried. I need when i click the button mNavigate to start the loop and run it with delay. I declare above that mNavMode is false

     mNavigate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d(TAG, "onClick: clicked NAV icon");
            mNavMode = !mNavMode;
            while (mNavMode){
                Log.d(TAG, "while: running");
                new CountDownTimer(10000, 2500) {
                    public void onFinish() {
                        Log.d(TAG, "onFinish: finish");
                    }

                    public void onTick(long millisUntilFinished) {
                        if (mNavMode){
                            getDevLoc();
                        }
                        Log.d(TAG, "onTick: ticked "+millisUntilFinished);
                    }
                }.start();
            }
        }
    });



  mNavigate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d(TAG, "onClick: clicked NAV icon");
            mNavMode = !mNavMode;
            Handler handler1 = new Handler();
            while (mNavMode){
                handler1.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        getDevLoc();
                    }
                }, 1000 );
                Log.d(TAG, "while: running");
            }
        }
    });

1 Answers1

0

I've used the second option using a Handler, it should work. But it appears that your while condition is false, just before starting a loop you set mNavMode = !mNavMode, and if your mNavMode is set to true beforehand than your loop body is never executed.

Suleyman
  • 2,765
  • 2
  • 18
  • 31
  • mNavmode is set to false before hand i added that now sorry – Antreas Papavasiliou May 07 '18 at 19:59
  • @AntreasPapavasiliou Try checking if the loop body is executed, and also, how do you know that the delay doesn't work? it's only one second in your case, maybe you don't notice it, try increasing it. – Suleyman May 07 '18 at 20:02
  • when i run my app and press the button i get the in the logcat the "while: running" more than 10 times a second – Antreas Papavasiliou May 07 '18 at 20:10
  • try moving the `Log` statement inside the `run` function – Suleyman May 07 '18 at 20:16
  • @AntreasPapavasiliou maybe you got the wrong `import` statement, make sure that you have `import android.os.Handler;` – Suleyman May 07 '18 at 20:28
  • @AntreasPapavasiliou There is obviously something that I don't understand, try [this](https://stackoverflow.com/questions/34533545/how-can-i-delay-onclick-action) question meanwhile, I'll try to recreate the problems. – Suleyman May 07 '18 at 20:51
  • @AntreasPapavasiliou add `final` to your `Handler` so it's `final Handler handler1 = new Handler();` and after your `getDevLoc();` inside `run` add this line: `handler.postDelayed(this, 1000);` – Suleyman May 07 '18 at 21:09