Activity Loading again and again

Viewed 65

I'm trying to open another activity when the date arrives else the seconds left for date are displayed but the problem is when i'm running app it is loading same page again and again .

But there is no Problem when i'm running from blast activity but when i run it from countdown activity blast activity is opened again and again as the date arrives . I mean future date is more then or equal to given date .

Here is Countdown activity below :

public void countDownStart() {
        handler = new Handler();
        runnable = new Runnable() {
            @Override
            public void run() {
                handler.postDelayed(this, 1000);

                // using try and catch for error handling
                try {
                    SimpleDateFormat dateFormat = new SimpleDateFormat(
                            "yyyy-MM-dd");
       
                    // Please here set your event date//YYYY-MM-DD
                    Date futureDate = dateFormat.parse("2021-04-12");
                    
                    Date currentDate = new Date();
                    if (!currentDate.after(futureDate)) {
                        long diff = futureDate.getTime()
                                - currentDate.getTime();
                        long days = diff / (24 * 60 * 60 * 1000);
                        diff -= days * (24 * 60 * 60 * 1000);
                        long hours = diff / (60 * 60 * 1000);
                        diff -= hours * (60 * 60 * 1000);
                        long minutes = diff / (60 * 1000);
                        diff -= minutes * (60 * 1000);
                        long seconds = (diff / 1000)+(minutes*60)+(hours*60*60)+(days*60*60*60);
                        text1.setText("" + String.format(FORMAT, seconds));
                    } else {
                        Intent intent = new Intent(CountDown.this,BlastActivity.class);
                        startActivity(intent);
                        finish();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        };
        handler.postDelayed(runnable, 1 * 1000);
    }

Here is Blast Activity below :

public class BlastActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
//        Playing audio on activity open
        MediaPlayer birthday = MediaPlayer.create(BlastActivity.this, R.raw.birthday);
        birthday.start();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.blast);

        // Delaying the next activity to be executed to wait for song finish
        new Timer().schedule(new TimerTask() {

            @Override
            public void run() {
                Intent intent = new Intent(BlastActivity.this, MainActivity.class);
                birthday.stop();                // Song stopped when moving to next activity
                startActivity(intent);
                finish();
            }
        }, 20000);
    }


}

Here is Android MainFest file code :

<activity android:name=".BlastActivity">
        </activity>
        
        <activity android:name=".CountDown" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MainActivity" android:launchMode="singleTask"/>

Blast Activity is loading again and again whenever I'm opening the app .

2 Answers

Well, the problem is that the Handler keeps executing the Runnable although you've destroyed the Activity. The easiest solution is to add another instance variable as below,

boolean terminateTimer = false;

and check this before posting the runnable to the Handler.

runnable = new Runnable() {
  public void run() {

    if (terminateTimer) // add this
      return;

    handler.postDelayed(this, 1000);
    try {
      ...
      else {
        terminateTimer = true;
        ...
      }
    } catch (Exception e) { ... }
  }
};

Change the value to true when you need to stop the timer.

public void onDestroy() {
  terminateTimer = true;
  ...
}

You need to remove the callbacks from handler before starting new Activity. You can do like this

handler.removeCallbacksAndMessages(null);
Related