Too many Fragments causing OutOfMemory error - android

I'm making an app that uses a bottom navigation bar (BottomBar) on its MainActivity. A FrameLayout is positioned above the BottomBar. Every time an icon in the bottom bar is clicked, the tab's fragment is inserted into the FrameLayout using FragmentTransaction's replace() function. That inserted fragment contains ViewPager, which results in more fragment launching and caching. Right now, there is only one Activity but many Fragments. I haven't load any data and I'm already receiving a OutOfMemory Exception even after setting the maxHeapSize to 4gb.
What can I do to improve the performance without changing the UI (still have a bottom navigation bar and each tab contains a viewpager)?

Related

problem with viewpager2 and bottomnav working together in android studio

so I'm trying to make small application type of thing where it has bottom navigation bar and viewpager2 working in it. But I want it so that in the first fragment page that application loads in, only there viewpager2 is available to work and be able to swipe, when I switch to next fragment with navigation bar that viewpager2 should basically disappear and not work, therefore my logic was to put that viewpager2 in that single fragment and navigation bar outside in main activity for it to be able to be seen in every fragment. But there is a problem, it almost works, I switch to another fragment with bar view pager disappears but when I go back to that first fragment view pager is just gone, disappeared and I have no idea what's the problem. if necessary I can show what I have written. Thanks in advance

Keep fragment's state when changing tabs in bottom bar

Currently I have a ViewPager with 4 Tabs, each one is a different Fragment. I'm now switching my app to Bottom Bar Navigation but there's a problem with keeping the Fragment state and re-using it later.
The Problem :
Initially Tab_1 is visible. If user selects Tab_2 and then re-selects Tab_1, then I can see that the Fragment for Tab_1 goes through onCreate() and re-downloads the data from the server. With ViewPager I was using the setOffScreenPagesLimit() is there something equivalent with bottom bar ?

Android load fragment content only when it is visible

I have a single activity which has a tabPageIndicator at the bottom of the screen. . The first four icons need to open other fragments. The final icon needs to open an alert box with options to open other screens.
I am facing two problems:
ViewPager loads all the 4 fragments at once and there is a lot of data being processed in the background. I would like to know if there is any way to load content only when the fragment is visible to user. I tried using setPageLimit but it did not work.
My second problem is that I would like to ideally open a popup when the last icon is clicked, rather than opening another fragment. IS that possible?
I am not sure if viewPager supports view without fragments.

Fragment layout loading delay

As per the answer here How to make fragments load faster? I put all tasks in an async task and still there's a delay while loading fragment. The fragment is a calculator, with 30 buttons and two text views.
I have a sliding drawer and the delay occurs when I choose the item that opens the calculator fragment. The sliding drawer lags while returning to hidden position.
I use fragmentTransaction.replace(R.id.content_frame, calc_frag) in a fragment activity to load the fragment.
I removed all the code in the fragment except inflating the layout. Lag.
How I get rid of the delay/lag?
Without code its hard to say, maybe you should look at ProgressBar and hold on showing the fragment until the contents have fully loaded?

Android changing setNavigationMode per fragment is crashing the app

I created the below project so you can see my exact code and what is going on:
https://github.com/CorradoDev/TabsTest/commit/8f054dab2371b791c4061ceb511413f720f65d67
Basically what I am trying to do is hide the tabs for some pages and show them in other pages.
Below is the code I am using to show the tabs in the onresume
if(getActivity().getActionBar().getNavigationMode()==ActionBar.NAVIGATION_MODE_STANDARD){
getActivity().getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
}
THen to hide the tabs I am doing the below on resume:
getActivity().getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
When I am on the first fragment(nothing in backstack). I can show and hide the tabs on hte second. It gives errors sometimes with changing tabs.
When I am on the second fragment in the backstack and I hide the third fragment. I see the second and third fragment both call the onrefresh but the third fragment does not show.
I am confused on what is going on and why this is not easier.
Below is the error I generally get
03-27 15:26:31.029: E/AndroidRuntime(5505): java.lang.IllegalStateException: Fragment already added: Fragment3{41f2e390 #2 id=0x1020002 fragment3}
I still would like to know why the above does not work. But my fix was to create another activity with the fragment and no tabs. That seems to work well. But I am interested if they did not intend you to change tabs and no tabs per fragment.
I had a similar situation - only that I used NAVIGATION_MODE_LIST instead of tabs. I run into similar issues when I called a fragment from another fragment e.g. click on a list item opening up the item details.
Now I call all fragments from the main activity which allows me to control the set up of the actionbar. Whenever the navigation list should disappear I just call NAVIGATION_MODE_STANDARD when the fragment is called and NAVIGATION_MODE_LIST for the other fragments.

Categories

Resources