diff --git a/app/src/main/java/org/schabi/newpipe/about/LicenseFragment.java b/app/src/main/java/org/schabi/newpipe/about/LicenseFragment.java index 272e27240..4f6a69dcc 100644 --- a/app/src/main/java/org/schabi/newpipe/about/LicenseFragment.java +++ b/app/src/main/java/org/schabi/newpipe/about/LicenseFragment.java @@ -1,5 +1,6 @@ package org.schabi.newpipe.about; +import android.app.Activity; import android.content.Context; import android.content.Intent; import android.net.Uri; @@ -39,7 +40,7 @@ public class LicenseFragment extends Fragment { * @param license the license to show */ public static void showLicense(Context context, License license) { - new LicenseFragmentHelper().execute(context, license); + new LicenseFragmentHelper((Activity) context).execute(license); } @Override diff --git a/app/src/main/java/org/schabi/newpipe/about/LicenseFragmentHelper.java b/app/src/main/java/org/schabi/newpipe/about/LicenseFragmentHelper.java index 726e97ec2..e4436322a 100644 --- a/app/src/main/java/org/schabi/newpipe/about/LicenseFragmentHelper.java +++ b/app/src/main/java/org/schabi/newpipe/about/LicenseFragmentHelper.java @@ -1,8 +1,11 @@ package org.schabi.newpipe.about; +import android.app.Activity; import android.content.Context; import android.content.DialogInterface; import android.os.AsyncTask; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.support.v7.app.AlertDialog; import android.webkit.WebView; import org.schabi.newpipe.R; @@ -10,26 +13,46 @@ import org.schabi.newpipe.util.ThemeHelper; import java.io.BufferedReader; import java.io.InputStreamReader; +import java.lang.ref.WeakReference; public class LicenseFragmentHelper extends AsyncTask { - private Context context; + WeakReference weakReference; private License license; + public LicenseFragmentHelper(@Nullable Activity activity) { + weakReference = new WeakReference<>(activity); + } + + @Nullable + private Activity getActivity() { + Activity activity = weakReference.get(); + + if (activity != null && activity.isFinishing()) { + return null; + } else { + return activity; + } + } + @Override protected Integer doInBackground(Object... objects) { - context = (Context) objects[0]; - license = (License) objects[1]; + license = (License) objects[0]; return 1; } @Override protected void onPostExecute(Integer result){ - String webViewData = getFormattedLicense(context, license); - AlertDialog.Builder alert = new AlertDialog.Builder(context); + Activity activity = getActivity(); + if (activity == null) { + return; + } + + String webViewData = getFormattedLicense(activity, license); + AlertDialog.Builder alert = new AlertDialog.Builder(activity); alert.setTitle(license.getName()); - WebView wv = new WebView(context); + WebView wv = new WebView(activity); wv.loadData(webViewData, "text/html; charset=UTF-8", null); alert.setView(wv); diff --git a/app/src/main/java/org/schabi/newpipe/info_list/InfoListAdapter.java b/app/src/main/java/org/schabi/newpipe/info_list/InfoListAdapter.java index 9b3405484..cf12deb6f 100644 --- a/app/src/main/java/org/schabi/newpipe/info_list/InfoListAdapter.java +++ b/app/src/main/java/org/schabi/newpipe/info_list/InfoListAdapter.java @@ -17,6 +17,7 @@ import org.schabi.newpipe.info_list.holder.PlaylistInfoItemHolder; import org.schabi.newpipe.info_list.holder.PlaylistMiniInfoItemHolder; import org.schabi.newpipe.info_list.holder.StreamInfoItemHolder; import org.schabi.newpipe.info_list.holder.StreamMiniInfoItemHolder; +import org.schabi.newpipe.util.FallbackViewHolder; import org.schabi.newpipe.util.OnClickGesture; import java.util.ArrayList; @@ -238,7 +239,7 @@ public class InfoListAdapter extends RecyclerView.Adapter { + private static class ChecksumTask extends AsyncTask { ProgressDialog prog; + WeakReference weakReference; + + ChecksumTask(@NonNull Activity activity) { + weakReference = new WeakReference<>(activity); + } @Override protected void onPreExecute() { super.onPreExecute(); - // Create dialog - prog = new ProgressDialog(mContext); - prog.setCancelable(false); - prog.setMessage(mContext.getString(R.string.msg_wait)); - prog.show(); + Activity activity = getActivity(); + if (activity != null) { + // Create dialog + prog = new ProgressDialog(activity); + prog.setCancelable(false); + prog.setMessage(activity.getString(R.string.msg_wait)); + prog.show(); + } } @Override @@ -374,8 +386,24 @@ public class MissionAdapter extends RecyclerView.Adapter