How to manage memory with too many items on android recycleview - java

I am developing an android social media type application, where I have a multiple tab activity. The tabs are fragments using viewpager.I have a home screen with a recycler view. The post have images on them. I am having memory problem with the recyclerview for post. I am using firebase as a database if thats helpful. To load the imageview I am using glide and I am doing diskcache and skipping the memory caches.
Issues I am having are
When too many posts are loaded, recyclerview becomes slow and laggy (I know that there are other post with this but couldnt really found anything helpful).
When I click on a username from the post, a new profile activity starts which also have all the posts from that user, I am trying to find a way clear the homepage tab recyclerview memory but struggleing with how to.
To solve the first problem, I have set up an onscrolllistner that loads 10 new post everytime you are near the end of the post list. The recycler view adapter currently loads 20 items initially and then so on.but I am not sure how to clear the old posts in an efficient way since I want the user to be able to scroll up.
To solve the second problem, In the homescreen fragment, when I launch the new activity, the onPause() function gets called. In the onpause() function I tried setting the list to empty, recyclerview adapter to null, recylerview to null but nothing has worked. In the android profiler tool, I see that I am using 200 mb memory, and when I start the new activity, it adds another 100 mb or so. I tried manual garbage collections but it doesnt work either. To be specific, native memory jumps up in the profiler. Ive tested and made sure that was only because of the images in the recyclerview.
I am not sure why when I set recyclerview, adapter and the post list to null, why the images from posts are still being held in the memory.

Glide caches the images
To load multiple post check paging library
When moving to detail fragment and clearing the reacyclerview of home fragment , I would not recommend this as next time user switches to home tab ,the user will not have previous state saved which he left previously.

Related

RecyclerView leaves residue on scroll

I have a big activity (let's call it BigActivity) where I have a lot fragments inside. From BigActivity I start another one (let's call it PainfulActivity) with an Intent the usual way. I have a fragment living inside PainfulActivity and a RecyclerView in the fragment (let's call the fragment PainfulFragment).
In my PainfulFragment I make a network call and fetch my data. I have an adapter (let's call that PainfulAdapter) that I subclassed from RecyclerView.Adapter.
And successfully wired up everything. Data is drawn on the screen and no problem until now. When I scroll RecyclerView, it leaves residue or glitch whatever you call it.
I tried to populate my PainfulFragment in my BigActivity in one of the fragment containers, and everything works fine. So I know my fragment, adapter etc.. is working fine.
I tried to populate one of the fragments in PainfulActivity that normally lives in BigActivity, and I have the same problem, RecyclerView glitches again on scroll.
I was thinking perhaps it could be a memory problem, and observed the Profiler tool in Android Studio, however there is nothing unusual there neither.
Did anyone come across with such a problem?

Start ViewPager from RecyclerView item

Basically what the title says. I'm trying to start a ViewPager activity when a user clicks a RecyclerView item. I'm not having problems here, that works fine. The goal is to go from a grid based gallery to viewpager gallery. I'm also trying to send the List<> of data which is loaded over the network. This is where I'm having a lot of trouble. The data is sent through a singleton because it's too large to send through intent and if opened in the new activity too quickly upon app startup, more data is loaded in the original activity (Even if Call is cancelled, using Retrofit2 btw) and I need to notify the adapter, which I have a listener that does.
Another crash that is common is a null pointer when reading an item in viewpager activity, which doesn't make sense to me because the item is not null in the original activity before sending, but if I wait a few seconds it is not null. Some of the code pertaining to my issue can be found here, a question I asked yesterday with a solution I could not successfully implement.
Please point me in the right direction for starting a ViewPager of the same dataset from a RecyclerView, thanks
When you are "sending" feed data to the view pager activity, use a shallow copy of the list instead of directly referencing it as shown below
DataTransferer.get().storeItems(new ArrayList(feed));
This should prevent IllegalStateException raised by feed being updated on your RecyclerView activity.

how to make images redrawn on imageView inside ListView

I have a ListView that is like an inbox. The information comes from a user,so the ListView displays text and a userPicture.To display userPictures, I display the userPicture if exists or a imageView of a letter of the first name.
when I receive data from WS I launch an asynctask that compare userPicture ids with DB to ask the WS for ones I don't have or one's that have changed.
After the asyncTask finishes I save pictures locally and notify listView that the data set has changed, in order to look for the new pictures.
my problem comes when there is only 1 user on the listview and the picture wasn't found but was downloaded from WS. after calling notifyDataSetChanged, also calling refreshDrawableState and invalidateViews,that acording to documentation "Causes all the views to be rebuilt and redrawn". the image is not changed and imageView still shows the letter ImageView,
neither scrolling through the listView its updated. (I guess is part of the Viewholder that recycle the views that is meant for performance and a good practive acording to ).
on the getView of the adapter I have a the Following statement to processThe Picture
if(mIDataPersistence.userHasPicture(userId)
ViewHolder.userImg.setImageBitmap(mIDataPersistence.getUserPicture(userId));
else
ViewHolder.userImg.setImageBitmap(BitmapUtils.createBitMap(userName);
note: mIDataPersistence is persistence data interface to access all the data locally stored that I'm using here. and BitmapUtils.createBitMap() makes a bitmap from the first letter of the user.
can anyone help me or point me on a way to make listView update images in this particular case.

Recycling Activity Views on the Activity-Back-Stack

I have an application that is very image-intense, and I'm finding that I'm running into alot of OutOfMemory issues when loading up multiple activities.
The activities are all gridviews or listviews of bitmaps, and clicking on an image takes you to another activity that contains another set of images (sort of like an album of albums of albums of albums). The first 3 activities run fine, but when navigating further down, I start running into some serious OutOfMemory Errors.
After doing a stack dump with DDMS, it appears that the GridViews and Listviews of the previous activities are hogging all the memory. This is kind of expected, since they are showing lots of bitmaps themselves, but I'm not sure how the VM goes about freeing activities in the stack, and if they do it even when you havn't called "finish()" on them.
Do Activities recylce their views when they go into the background and then restore them when you navigate back to the activity, or is clearing the imageviews in my gridviews and listviews this something I need to handle manually on the activities onPause() and onResume() when navigating away from the activity in the lifecycle functions?
Please see my answer on OOME
Always call bitmap.recycle() after using bitmaps, since GC cannot clear memory held by Bitmaps.
The above link has a generic solution, please go through it.
consider recycling bitmaps onPause

View different images depending on what item in my list view I click

I'm fairly new to Android programming and I've got this project I need to finish and I'm currently stuck.
I've got a standard listview in a Menu class with an array containing around 20 different elements. So what I want to do is load images in an imageview depending on which item in the listview I click, and since I'm a beginner the only idea I had was to make a new activity for each imageview which seems like a pretty bad way to do it since I'd need about 20-30 new activities..
To sum things up what I want is:
Code for making ONE activity that will display a different image depending on which item in the listview I click, probably pretty basic coding I want as simple solution as possible.
If possible I'm also looking for a solution that includes an SQLite database that stores the URL of an image and then display it in a single activity, also depending on which item I press in my current listview.
(I hope you understand my needs, and if you need I can also post my current code for the Menu class if it helps you help me) Or you can just show me a different way to make this work, I appreciate every answer! Thanks in advance!
NOTE
And please keep in mind, I'm a noob at Java and Android so keep it rather simple or at least explain what you do.
When you click on a list item, display the image in another view in the same layout, unless you want the image to take up the entire screen real estate. If you want it in the entire screen, go to a new Activity by sending the activity an Intent.
Activities are the "controller" of your application. They interact with the visible UI and the input from the user. You don't need a separate activity for each image, just an activity that's associated with a "place" in the UI (an ImageView) where you'll display the image.
I'd start by adding the images as resources under res/drawable before going on to databases.
You are going to have to do most of this yourself. There really isn't any substitute for taking the time to learn Java and Android. There are several tutorials and Android University classes under the Resources tab in the Developers Guide; I suggest you do all of them.

Categories