Reputation: 4283
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
Upvotes: 1
Views: 37
Reputation: 6697
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
}
Upvotes: 2