1

This code, contained in QuizFragment.java, delays for ms milliseconds and then executes loadNextFlag. Works fine. (ms = 2000 milliseconds)

public void pauseAndThenShowNextFlag(long ms){
    showTimer("pauseAndThenShowNextFlag");
    pauseTimer(); // 7/17/15--DAS
    handler.postDelayed
            (
                    new Runnable() {
                        @Override
                        public void run() {
                            loadNextFlag();
                        }
                    }, ms       // ms-second delay
            );
    startTimer();
}

I changed the delay to be in another object, so then the above became:

public void pauseAndThenShowNextFlag(long ms){
    showTimer("pauseAndThenShowNextFlag");
    Timer.xhaltTimer4(ms);
    loadNextFlag();
    startTimer();
}

Here's xhaltTimer4 in Timer.java:

public static void xhaltTimer4(long ms){
    customHandler.postDelayed
            (
                    new Runnable() {
                        @Override
                        public void run() {
                            pauseTimer();
                        }
                    }, ms); // 2000 milliseconds for 2-second delay
}

Absolutely no delay occurs. Is it because loadNextFlag contains a lot more code than pauseTimer? Why does that make a difference? Where is the line drawn in terms of how much the Runnable run method must do to enable the delay?

I tried this revised xhaltTimer4--still no delay:

public static void xhaltTimer4(long ms){
    Handler h = new Handler();
    h.postDelayed
            (
                    new Runnable() {
                        @Override
                        public void run() {
                            pauseTimer();
                        }
                    }, ms); // ms-millisecond delay
}

Here's pauseTimer:

public static void pauseTimer(){
    timeSwapBuff += timeInMilliseconds;
    customHandler.removeCallbacks(updateTimerThread);
}

Here's loadNextFlag:

public void loadNextFlag() {
    startTimer();
    String timerDisplayString;
    oneImageView.setVisibility(View.INVISIBLE);
    showTimer("loadNextFlag");
    currentTimeSpent = timeStringToMillis(Timer.getTimerString());// was prevTimeSpent--7/17/15--DAS
    InputStream stream ;
    // get file name of the next flag and remove it from the list
    consecutiveWrong = 0;

    String nextImage = quizFlagPathList.remove(0);
    correctAnswer = nextImage; // update the correct answer
    answerImageView.setVisibility(View.INVISIBLE);
    ableButtons(true);

    // display current question number--2nd and 3rd parameters are INPUT into the xml statement
    questionNumberTextView.setText(getResources().getString(R.string.question,
            (correctAnswers + 1),
            FLAGS_IN_QUIZ));
    // extract the region from the next image's name
    String region = nextImage.substring(0, nextImage.indexOf('-'));

    // use AssetManager to load next image from assets folder
    AssetManager assets = getActivity().getAssets();

    try{
        stream = assets.open(region + "/" + nextImage + ".png");

        Drawable flag = Drawable.createFromStream(stream, nextImage);
        flagImageView.setImageDrawable(flag);
    }catch (IOException exception){Log.w(TAG, "Error loading " + nextImage, exception);}

    Collections.shuffle(flagPathList); // shuffle file names

    // put the correct answer at the end of flagPathList
    int correct = flagPathList.indexOf(correctAnswer);

    flagPathList.add(flagPathList.remove(correct));

    int k = 0;
    countriesToUse.clear();
    for(int row = 0; row < guessRows; row++)
        for (int col = 0; col < guessLinearLayouts[row].getChildCount(); col++)
            countriesToUse.add(getCountryName(flagPathList.get(k++)));

    countriesToUse.set(countriesToUse.size() - 1, getCountryName(flagPathList.get(flagPathList.size() - 1)));

    if(MainActivity.getAlphabetize()) Collections.sort   (countriesToUse);
    else                              Collections.shuffle(countriesToUse);

    k = 0;
    // add 3, 6, or 9 guess Buttons based on the value of guessRows
    for (int row = 0; row < guessRows; row++) {
        // place Buttons in currentTableRow
        for (int column = 0; column < guessLinearLayouts[row].getChildCount(); column++) {
            // get reference to Button to configure
            Button newGuessButton = (Button) guessLinearLayouts[row].getChildAt(column);
            newGuessButton.setEnabled(true);
            // get country name and set it as newGuessButton's text
            String fileName = countriesToUse.get(k++);
            newGuessButton.setText((fileName));
        }
    }
} // end method loadNextFlag

1 Answer 1

2

This is because xhaltTimer4 is synchronous method call, it won't wait for ms. Inside this you are posting one Runnable,which will run after some delay. You should try to move loadNextFlag() inside run() method of runnable like this

public static void xhaltTimer4(long ms){
    Handler h = new Handler();
    h.postDelayed
            (
                    new Runnable() {
                        @Override
                        public void run() {
                            pauseTimer();
                            loadNextFlag()
                        }
                    }, ms); // ms-millisecond delay
}
3
  • Yeah, I would like to, but it's not a static method and can't be made so. I'd like to pass loadNextFlag as a parameter somehow and use it here, but I doubt that's even possible. I guess I could move xhaltTimer4 to the class where the call is made...
    – DSlomer64
    Jul 18, 2015 at 16:35
  • Why I'm asking is that I'm having trouble implementing a working Timer class that I found/stole/modified. My thinking was that the QuizFragment class contains a Handler that causes the delay specified before the next flag is shown but my Timer class has its own Handler to keep track of time spent by user on current flag. I thought reducing to one Handler would maybe fix the problem. I think I should drop this question and fix what the actual problem is in that code and maybe ask a different question.
    – DSlomer64
    Jul 18, 2015 at 17:06
  • Sorry.. but i didn't get what you are trying to acheive. May be specific question will help
    – Akhil
    Jul 18, 2015 at 17:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.