0

i am developing an android game , i have two methods for the gameplay that the one calls the others repeatable after every round. When i minimized the app from mobile i can hear the sounds from the app, that means that the methods keep running.What do you think is the proper way to "kill" this activity when the app minimized

UPDATE: I can understand that i must do this in onPause ,but the question is what is the proper way to stop the loop(the two methods calls its other repeatable) from running in on pause?I must do this with a global variable to inform the loop to stop, or is another way to stop all the activity from running ?

UPDATE: First method:

           protected void playPattern() throws IOException {
                  checkList.clear();
                  patternList.clear();
                  Log.e("Debug", "start playpattern");
                  switch (turn) {
                   case 0:
                    Log.e("Debug", " playpattern case 0");
                    handlerLa.postDelayed(new Runnable() {
                      @Override
                      public void run() {
                      noteNumnber = 10;
                      noteNumnber = r.nextInt(4);
                      playNote(noteNumnber);
                    }
                   }, 800);
                   change.postDelayed(new Runnable() {
                   @Override
                   public void run() {
                      changeColor(noteNumnber);
                      checkPattern();
                   }
                 }, 1400);
                 break;
                case 1:
                   Log.e("Debug", " playpattern case 1");
                   handlerLa.postDelayed(new Runnable() {
                   @Override
                   public void run() {
                       Log.e("Debug", " playpattern case 1 prwti nota");
                       changeColor(noteNumnber);
                       noteNumnber = 10;
                       noteNumnber = r.nextInt(4);
                       Log.e("Debug", " playpattern case 1 prwti nota" + " >>" + noteNumnber);
                       playNote(noteNumnber);

                   }
                 }, 800);
                handlerRe.postDelayed(new Runnable() {
                @Override
                public void run() {
                    changeColor(noteNumnber);
                    noteNumnber = 10;
                    noteNumnber = r.nextInt(4);
                    Log.e("Debug", " playpattern case 1 deferi nota" + " >>" + noteNumnber);
                    playNote(noteNumnber);

                  }
               }, 1200);
               change.postDelayed(new Runnable() {
                @Override
                 public void run() {
                    changeColor(noteNumnber);
                    checkPattern();
                   }
              }, 1400);
              break;
           case 2:
               Log.e("Debug", " playpattern case 2");
               handlerLa.postDelayed(new Runnable() {
                  @Override
                  public void run() {
                    changeColor(noteNumnber);
                    noteNumnber = 10;
                    noteNumnber = r.nextInt(4);
                    playNote(noteNumnber);

                }
            }, 800);

            handlerRe.postDelayed(new Runnable() {
                @Override
                public void run() {
                    changeColor(noteNumnber);
                    noteNumnber = 10;
                    noteNumnber = r.nextInt(4);
                    playNote(noteNumnber);

                }
            }, 1200);

            handlerMi.postDelayed(new Runnable() {
                @Override
                public void run() {
                    changeColor(noteNumnber);
                    noteNumnber = 10;
                    noteNumnber = r.nextInt(4);
                    playNote(noteNumnber);
                }
            }, 1600);
            change.postDelayed(new Runnable() {
                @Override
                public void run() {
                    changeColor(noteNumnber);
                    checkPattern();
                }
            }, 1800);
            break;
        case 3:
            Log.e("Debug", " playpattern case 3");
            handlerLa.postDelayed(new Runnable() {
                @Override
                public void run() {
                    changeColor(noteNumnber);
                    noteNumnber = 10;
                    noteNumnber = r.nextInt(4);
                    playNote(noteNumnber);

                }
            }, 800);

            handlerRe.postDelayed(new Runnable() {
                @Override
                public void run() {
                    changeColor(noteNumnber);
                    noteNumnber = 10;
                    noteNumnber = r.nextInt(4);
                    playNote(noteNumnber);

                }
            }, 1200);

            handlerMi.postDelayed(new Runnable() {
                @Override
                public void run() {
                    changeColor(noteNumnber);
                    noteNumnber = 10;
                    noteNumnber = r.nextInt(4);
                    playNote(noteNumnber);

                }
            }, 1600);
            handlerDof.postDelayed(new Runnable() {
                @Override
                public void run() {
                    changeColor(noteNumnber);
                    noteNumnber = 10;
                    noteNumnber = r.nextInt(4);
                    playNote(noteNumnber);

                }
            }, 2000);
            change.postDelayed(new Runnable() {
                @Override
                public void run() {
                    changeColor(noteNumnber);
                    checkPattern();
                }
            }, 2200);
            break;

    }

}

second method :

               protected void checkPattern() {
                  waitPlayer.postDelayed(new Runnable() {
                  @Override
                  public void run() {
                     if (checkList.equals(patternList)) {
                         Log.e("Debug", "Correct");
                          turn++;
                          try {
                             playPattern();
                         } catch (IOException e) {
                            e.printStackTrace();
                        }
                   } else {
                        Log.e("Debug", "InCorrect");
                        try {
                            playPattern();
                        } catch (IOException e) {
                         e.printStackTrace();
                     }
                 }
        }
    }, 6000);

}
Thelouras
  • 852
  • 1
  • 10
  • 30

3 Answers3

2

Read more about the Android Lifecycle. You can pause your action in the onPause method, and continue it in the onResume method.

Zun
  • 1,553
  • 3
  • 15
  • 26
  • This is what i think, but when the app is minimized the activity goes onPause ? In onPause what is the best way to stop this methods from running? with a global variable that it will informs the loop to stop or there is another way to stop all activity from running ? Thanks for your answer – Thelouras Apr 13 '18 at 11:31
  • I do not know what your application code looks like, but if you have a method called `startMusic()`, then you should make a `pauseMusic();` or `stopMusic();` method – Zun Apr 13 '18 at 11:33
  • is not the music the problem, is that the loop keep running, the sounds from the game is the way that i understand this. My question is how to stop the loop on onPause. – Thelouras Apr 13 '18 at 11:36
  • @Th.K How to stop the loop depends entirely on your code, so i suggest you post it, the relevant part that is – Isac Apr 13 '18 at 11:40
  • the one method call the other every time that the round doest change, so when i close the app the round doesnt grow up and the loop goes forever, this have as result that the loop play forever after minimize the app , how to stop it in on pause is my question – Thelouras Apr 13 '18 at 11:50
1

When an Activity goes to background or is minimized the lifecycle method void onPause() is called. You can put your code to stop the methods (stop sound, ...) there. If you don't have that method, create it in your activity:

@Override
public void onPause() {
  super.onPause();  // Always call the superclass method first
  // do what you want to do here
}

For android activity lifecycle, see https://developer.android.com/guide/components/activities/activity-lifecycle.html

martink
  • 152
  • 5
  • This is what i think. In onPause what is the best way to stop this methods from running? with a global variable that it will informs the loop to stop or there is another way to stop all activity from running ? Thanks for your answer – Thelouras Apr 13 '18 at 11:33
1

I think you could stop music when app is in background, stopping it in OnPause or OnStop method(depends on which means minimize for you). You could also save the exact moment of music stopped in onSaveinstanceState method that is called after your activity goes in onPause. When the activity goes in foreground again in onResume method you re-play music or if activity went in onStop you could override OnRestoreInstanceState to restart music from the same moment in which it was stopped. I advice you to check activity life-cycle here.

UPDATE: Now that you put code, you should get the instance of your handler and remove callback in onPause. Go here and you will find the answer How to stop Handler Runnable?

Federico Rizzo
  • 183
  • 2
  • 9