فهرست منبع

Progress thread for progress sample will be interrupted when the Activity pauses, so the thread isn't leaked.

Aidan Follestad 10 سال پیش
والد
کامیت
b98eb7b34b
1فایلهای تغییر یافته به همراه22 افزوده شده و 3 حذف شده
  1. 22 3
      sample/src/main/java/com/afollestad/materialdialogssample/MainActivity.java

+ 22 - 3
sample/src/main/java/com/afollestad/materialdialogssample/MainActivity.java

@@ -39,6 +39,7 @@ public class MainActivity extends AppCompatActivity implements
         FolderSelectorDialog.FolderSelectCallback, ColorChooserDialog.Callback {
 
     private Toast mToast;
+    private Thread mThread;
 
     private void showToast(String message) {
         if (mToast != null) {
@@ -49,6 +50,13 @@ public class MainActivity extends AppCompatActivity implements
         mToast.show();
     }
 
+    private void startThread(Runnable run) {
+        if (mThread != null)
+            mThread.interrupt();
+        mThread = new Thread(run);
+        mThread.start();
+    }
+
     private void showToast(@StringRes int message) {
         showToast(getString(message));
     }
@@ -237,6 +245,13 @@ public class MainActivity extends AppCompatActivity implements
         });
     }
 
+    @Override
+    protected void onPause() {
+        super.onPause();
+        if (mThread != null && !mThread.isInterrupted() && mThread.isAlive())
+            mThread.interrupt();
+    }
+
     private void showBasicNoTitle() {
         new MaterialDialog.Builder(this)
                 .content(R.string.shareLocationPrompt)
@@ -479,6 +494,7 @@ public class MainActivity extends AppCompatActivity implements
                 }).build();
 
         positiveAction = dialog.getActionButton(DialogAction.POSITIVE);
+        //noinspection ConstantConditions
         passwordInput = (EditText) dialog.getCustomView().findViewById(R.id.password);
         passwordInput.addTextChangedListener(new TextWatcher() {
             @Override
@@ -534,6 +550,7 @@ public class MainActivity extends AppCompatActivity implements
     @Override
     public void onColorSelection(int index, int color, int darker) {
         selectedColorIndex = index;
+        //noinspection ConstantConditions
         getSupportActionBar().setBackgroundDrawable(new ColorDrawable(color));
         ThemeSingleton.get().positiveColor = color;
         ThemeSingleton.get().neutralColor = color;
@@ -627,10 +644,11 @@ public class MainActivity extends AppCompatActivity implements
                         @Override
                         public void onShow(DialogInterface dialogInterface) {
                             final MaterialDialog dialog = (MaterialDialog) dialogInterface;
-                            new Thread(new Runnable() {
+                            startThread(new Runnable() {
                                 @Override
                                 public void run() {
-                                    while (dialog.getCurrentProgress() != dialog.getMaxProgress()) {
+                                    while (dialog.getCurrentProgress() != dialog.getMaxProgress() &&
+                                            !Thread.currentThread().isInterrupted()) {
                                         if (dialog.isCancelled())
                                             break;
                                         try {
@@ -648,12 +666,13 @@ public class MainActivity extends AppCompatActivity implements
                                     runOnUiThread(new Runnable() {
                                         @Override
                                         public void run() {
+                                            mThread = null;
                                             dialog.setContent(getString(R.string.done));
                                         }
                                     });
 
                                 }
-                            }).start();
+                            });
                         }
                     }).show();
         }