-1

I'm using Glide to load the image from the internet in my android app. When the user click on the log out button, I'm logging out the user by clearing all the shared preferences and restarting the app by using the following code:

Intent i = getActivity().getBaseContext().getPackageManager().getLaunchIntentForPackage( getActivity().getPackageName() );
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);    
getActivity().finish();

and the logout is perfectly working. But, when the user login again, My app get crashed.

Here is the log details:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.highavenue.android, PID: 21503
              java.lang.IllegalArgumentException: You cannot start a load for a destroyed activity
                  at com.bumptech.glide.manager.RequestManagerRetriever.assertNotDestroyed(RequestManagerRetriever.java:134)
                  at com.bumptech.glide.manager.RequestManagerRetriever.get(RequestManagerRetriever.java:102)
                  at com.bumptech.glide.manager.RequestManagerRetriever.get(RequestManagerRetriever.java:87)
                  at com.bumptech.glide.Glide.with(Glide.java:629)
                  at com.highavenue.android.adapter.ActivityTimelineAdapter.onBindViewHolder(ActivityTimelineAdapter.java:181)
                  at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6673)
                  at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6714)
                  at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5647)
                  at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5913)
                  at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5752)
                  at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5748)
                  at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2232)
                  at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1559)
                  at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1519)
                  at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:614)
                  at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3812)
                  at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3529)
                  at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:4082)

Code : (where and how I used Glide)

Glide.with(context)
                .load(users_list.get(user_position).getProfileImageURL())
                .thumbnail(0.1f)
                .override(100, 100)
                .into(holder.profile_image);

the above code was used in a RecyclerViewAdapter class, where context refers to the context of a fragment.

Note: This exception not occurs when the user manually closes the app and open again.

3 Answers3

0

I believe this will be enough(Added Intent.FLAG_ACTIVITY_CLEAR_TASK):

Intent i = getActivity().getBaseContext().getPackageManager().getLaunchIntentForPackage( getActivity().getPackageName() );
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);  

However, as suggested in the same question in here, you can try using:

Glide.with(YourActivityName.this) // or `getApplicationContext()`

Instead of getting context. This will probably solve the error.

ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108
  • If I use the code u mentioned above, when the user click on back it will navigated to old activity (where I placed the logout button) –  Oct 07 '18 at 07:51
  • And why I'm not used My activity.this, because I'm loading the image on the recycler view..so if I use Myactivity.this it may lead to memory leak –  Oct 07 '18 at 07:52
  • Updated the answer. Please have a look. – ʍѳђઽ૯ท Oct 07 '18 at 08:00
  • Okay..I will try this and tell you –  Oct 07 '18 at 08:28
0

Try this

Glide.with(getApplicationContext()).load(...)
ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108
0

The error tells you the problem:

java.lang.IllegalArgumentException: You cannot start a load for a destroyed activity

So your Glide load call is executing after you have called finish() on the Activity.

You need to prevent calling the Glide load method after you've finished the Activity.

Given your current code, you could set a flag or something to tell the adapter to not load images:

mAdapter.disableImageLoading(); // Set a flag that internally prevents the Glide load from happening
Intent i = getActivity().getBaseContext().getPackageManager().getLaunchIntentForPackage( getActivity().getPackageName() );
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);    
getActivity().finish();

That's not the greatest solution, but best I can think of given the little I know of your code.

Hope that helps!

dominicoder
  • 9,338
  • 1
  • 26
  • 32