Skip to content

Commit

Permalink
prevent leak caused by ScheduledExecutorService.
Browse files Browse the repository at this point in the history
  • Loading branch information
takuaraki committed Feb 13, 2017
1 parent ff39dca commit 9bfd0be
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import io.github.droidkaigi.confsched2017.R;
Expand All @@ -21,6 +22,7 @@ public class SplashActivity extends AppCompatActivity {

private ActivitySplashBinding binding;
private final ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
private ScheduledFuture<?> scheduledFuture;
private final Handler handler = new Handler();
private final Runnable moveActivityRunnable = () -> {
scheduledExecutorService.shutdown();
Expand All @@ -37,7 +39,7 @@ protected void onCreate(Bundle savedInstanceState) {
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
}

scheduledExecutorService.scheduleAtFixedRate(
scheduledFuture = scheduledExecutorService.scheduleAtFixedRate(
() -> binding.particleAnimationView.postInvalidate(),
0L,
40L,
Expand All @@ -52,4 +54,9 @@ protected void onStop() {
handler.removeCallbacks(moveActivityRunnable);
}

@Override
protected void onDestroy() {
super.onDestroy();
scheduledFuture.cancel(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import io.github.droidkaigi.confsched2017.R;
Expand All @@ -19,6 +20,7 @@ public class SplashViewActivity extends AppCompatActivity {

private ActivitySplashBinding binding;
private final ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
private ScheduledFuture<?> scheduledFuture;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -30,11 +32,16 @@ protected void onCreate(Bundle savedInstanceState) {
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
}

scheduledExecutorService.scheduleAtFixedRate(
scheduledFuture = scheduledExecutorService.scheduleAtFixedRate(
() -> binding.particleAnimationView.postInvalidate(),
0L,
40L,
TimeUnit.MILLISECONDS);
}

@Override
protected void onDestroy() {
super.onDestroy();
scheduledFuture.cancel(true);
}
}

0 comments on commit 9bfd0be

Please sign in to comment.