# Android memo ## About the Problem Now, when the pause button is clicked, the following procedure will happen (in returnHomeCheck()) ### MediaPlayer 1. Pause the MediaPlayer 2. Save the time music have been played as a temporary variable. 3. **Only** after the positive button is tapped 3.1. Start the music from the beginning 3.2. Seek to the position saved at step2 If the positive button is not tapped, the saved position of music will disappear. That's why the music starts playing from the beginning in the bug. <details><summary>Corresponding Codes</summary><div> ```java= public void returnHomeCheck() { ... // step 1 MyMediaPlayer.player.pause(); // step 2 musicLength = MyMediaPlayer.player.getCurrentPosition(); ... final int finalMusicLength = musicLength; builder.setPositiveButton(R.string.pause_dialog_continue, new DialogInterface.OnClickListener() { // step 3 public void onClick(DialogInterface dialog, int id) { // step 3.1 MyMediaPlayer.player.start(); // step 3.2 MyMediaPlayer.player.seekTo(finalMusicLength); // restart the game loop resume(); } }); } ``` </div></details> ### Notes ( = The green circles we tap with music) 0. Now, we manage the timing of Notes by system date. 1. Record the current datetime (as the time the dialog shows up) 2. Stop the game loop (stop descending notes) 3. **Only** after the positive button is tapped 3.1. Record the current datetime (as the time the dialog closes) 3.2. Shift the "Game loop start time" 3.3. Restart the game loop (start descending notes according to a new "Game loop start time") <details><summary>Corresponding Codes</summary><div> ```java= public void returnHomeCheck() { // step 1 long dialogStartedAt = System.currentTimeMillis(); pause(); ... builder.setPositiveButton(R.string.pause_dialog_continue, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // step 2.1 long dialogEndedAt = System.currentTimeMillis(); // step 2.2 // Shift the started time to adjust the descending start timing. loopStartedAt += dialogEndedAt - dialogStartedAt; // restart the game loop resume(); } }); } ``` </div></details> ### What is the problem? Timing management system are **ONLY** called in a positive button listener function ### The solution - Call timing management system when the dialog is cancelled. - Forbid users to tap the outside of a dialog. - Users cannot cancel the dialog.