Method keep running after i minimize the application from mobile,proper way to stop it?












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);

}









share|improve this question





























    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);

    }









    share|improve this question



























      0












      0








      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);

      }









      share|improve this question
















      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);

      }






      android






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 1 at 9:21







      Thelouras

















      asked Apr 13 '18 at 11:26









      ThelourasThelouras

      5671719




      5671719
























          3 Answers
          3






          active

          oldest

          votes


















          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?






          share|improve this answer


























          • thanks , you have right the handler must stop! I will try it

            – Thelouras
            Apr 13 '18 at 12:07



















          4














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






          share|improve this answer
























          • 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

            – ZUNJAE
            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













          • i will put the code in the question in a second!!

            – Thelouras
            Apr 13 '18 at 11:41



















          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






          share|improve this answer
























          • 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











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f49815918%2fmethod-keep-running-after-i-minimize-the-application-from-mobile-proper-way-to-s%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          3 Answers
          3






          active

          oldest

          votes








          3 Answers
          3






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          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?






          share|improve this answer


























          • thanks , you have right the handler must stop! I will try it

            – Thelouras
            Apr 13 '18 at 12:07
















          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?






          share|improve this answer


























          • thanks , you have right the handler must stop! I will try it

            – Thelouras
            Apr 13 '18 at 12:07














          1












          1








          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?






          share|improve this answer















          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?







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Apr 13 '18 at 11:56

























          answered Apr 13 '18 at 11:39









          Federico RizzoFederico Rizzo

          8017




          8017













          • thanks , you have right the handler must stop! I will try it

            – Thelouras
            Apr 13 '18 at 12:07



















          • thanks , you have right the handler must stop! I will try it

            – Thelouras
            Apr 13 '18 at 12:07

















          thanks , you have right the handler must stop! I will try it

          – Thelouras
          Apr 13 '18 at 12:07





          thanks , you have right the handler must stop! I will try it

          – Thelouras
          Apr 13 '18 at 12:07













          4














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






          share|improve this answer
























          • 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

            – ZUNJAE
            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













          • i will put the code in the question in a second!!

            – Thelouras
            Apr 13 '18 at 11:41
















          4














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






          share|improve this answer
























          • 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

            – ZUNJAE
            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













          • i will put the code in the question in a second!!

            – Thelouras
            Apr 13 '18 at 11:41














          4












          4








          4







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






          share|improve this answer













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







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Apr 13 '18 at 11:27









          ZUNJAEZUNJAE

          6902718




          6902718













          • 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

            – ZUNJAE
            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













          • i will put the code in the question in a second!!

            – Thelouras
            Apr 13 '18 at 11:41



















          • 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

            – ZUNJAE
            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













          • i will put the code in the question in a second!!

            – Thelouras
            Apr 13 '18 at 11:41

















          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





          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

          – ZUNJAE
          Apr 13 '18 at 11:33





          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

          – ZUNJAE
          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





          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







          @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















          i will put the code in the question in a second!!

          – Thelouras
          Apr 13 '18 at 11:41





          i will put the code in the question in a second!!

          – Thelouras
          Apr 13 '18 at 11:41











          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






          share|improve this answer
























          • 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














          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






          share|improve this answer
























          • 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












          1








          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






          share|improve this answer













          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







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Apr 13 '18 at 11:33









          martinkmartink

          1224




          1224













          • 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



















          • 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

















          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





          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


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f49815918%2fmethod-keep-running-after-i-minimize-the-application-from-mobile-proper-way-to-s%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Monofisismo

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas