Skip to content

Commit

Permalink
Fix crashing on loading fragment when activity is destroyed
Browse files Browse the repository at this point in the history
  • Loading branch information
noelbautista91 committed Dec 3, 2016
1 parent c8d780d commit 679a4c1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@

import com.nbau21.animatedvectorsample.R;

import java.lang.ref.WeakReference;

public class LoadingScreenFragment extends Fragment {

int count;
ImageView ivLoading;
TextView tvLoading;
Button btnRestart;
DummyLoadingTask loadingTask;

public LoadingScreenFragment() {
}
Expand All @@ -33,20 +34,34 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_loading_screen, container, false);
ivLoading = (ImageView) view.findViewById(R.id.iv_loading);
btnRestart = (Button) view.findViewById(R.id.btn_restart);
tvLoading = (TextView) view.findViewById(R.id.tv_loading);

btnRestart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new DummyLoadingTask().execute(3);
}
});
new DummyLoadingTask().execute(5);
loadingTask = new DummyLoadingTask(view);
loadingTask.execute(5);
return view;
}

class DummyLoadingTask extends AsyncTask<Integer, Integer, String> {
@Override
public void onDestroy() {
loadingTask.cancel(true);
super.onDestroy();
}

static class DummyLoadingTask extends AsyncTask<Integer, Integer, String> {

int count;
ImageView ivLoading;
TextView tvLoading;

WeakReference<View> weakView;
View referencedView;

DummyLoadingTask(View view) {
weakView = new WeakReference<>(view);
referencedView = weakView.get();

ivLoading = (ImageView) referencedView.findViewById(R.id.iv_loading);
tvLoading = (TextView) referencedView.findViewById(R.id.tv_loading);
}

@Override
protected String doInBackground(Integer... params) {
Expand All @@ -63,23 +78,23 @@ protected String doInBackground(Integer... params) {

@Override
protected void onPostExecute(String result) {
if (referencedView == null) {

}

count = 0;
tvLoading.setText("Finished!");
ivLoading.setImageDrawable(getResources().getDrawable(R.drawable.animated_loading_final, null));
ivLoading.setImageDrawable(referencedView.getContext().getDrawable(R.drawable.animated_loading_final));
((Animatable) ivLoading.getDrawable()).start();
btnRestart.setVisibility(View.VISIBLE);

for (Drawable drawable : btnRestart.getCompoundDrawables()) {
if (drawable instanceof Animatable) {
((Animatable) drawable).start();
}
}
}

@Override
protected void onPreExecute() {
ivLoading.setImageDrawable(getResources().getDrawable(R.drawable.animated_loading, null));
btnRestart.setVisibility(View.INVISIBLE);
if (referencedView == null) {

}

ivLoading.setImageDrawable(referencedView.getContext().getDrawable(R.drawable.animated_loading));
((Animatable) ivLoading.getDrawable()).start();
tvLoading.setText("Loading..");
}
Expand All @@ -89,6 +104,4 @@ protected void onProgressUpdate(Integer... values) {
tvLoading.setText("Loading.. " + count + " seconds passed!");
}
}


}
10 changes: 0 additions & 10 deletions app/src/main/res/layout/fragment_loading_screen.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,4 @@
android:layout_below="@+id/iv_loading"
android:layout_centerHorizontal="true" />

<Button
android:layout_below="@+id/tv_loading"
android:drawableBottom="@drawable/animated_cat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Restart animation!"
android:id="@+id/btn_restart"
android:visibility="invisible"/>

</RelativeLayout>

0 comments on commit 679a4c1

Please sign in to comment.