Android load fragment content only when it is visible - android

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.

Related

Display slidingUpPanel across all activities

I'm trying to add a slidingUpPanel(Sliding up panel) across all activities. This panel consists of a viewpager which in turn consists of two fragments. One of these fragments has a recyclerView (more than 1000 items) and the other fragment has some data that changes dynamically depending on the users' choice. This panel is very similar to that of google play music and soundcloud. Now to display this panel I tested two approaches:
1) I created a base activity and added the sliding up panel to it and then extended rest of the activities to base activity. So this way I have to create only one panel and viewpager.
2) I included the sliding up panel in all activities. But this approach is quite unmanageable as I have create viewpagers for all activities and if data changes in one activity I have to write the code to reflect that change across all activities.
Now the problem with both the methods is that each time I open a new activity, a new instance of the viewpager and fragments is created. So, suppose if I have 1000+ items in the recyclerview fragment, switching activities takes more than 2-3 seconds, because each time new instance of fragment is created and the data is loaded all again. This will definitely result in bad user experience.
Is there any way by which the viewpager and the fragments are created only once(when the app starts) and are destroyed only when user closes the app? And data should not load each time user switches activities. I just want to reduce the activity switching time. Any ideas?
Thank you.
Well, for such ui elements as your sliding panel, which stays the same for many items it is preferable to have a single activity.
So if you have 1 activity, you can have a viewpager inside sliding panel, and that part stays untouched. Next, inside your activity you can have FrameLayout wich can be used to host fragments. Doing this you can achieve single instance of sliding panel and navigation between content with fragments.
Having some heavy data collections makes you wishing minimum recreation of that items.
Two approaches for this are (assuming you're using SlidingUpPanelLayout by sothree)
With bottom navigation view
Easy way to do it is creating a bottom navigation view and keeping it in MainActivity and attaching the sliding up panel layout to the bottom navigation view ( layout_above = bottomnavbar_id ) since bottom navigation stays throughout the app so sliding up panel will also have to stay with it
Without bottom navigation view
Create a frame layout inside MainActivity give attributes width and height as match_parent and create slidinguppanelayout and give attributes gravity="bottom"
make the frame layout stay above that slidinguppanellayout , use that frame_layout to show content your want to show from fragments
that's all

How to change images when navigate into next fragment?

I'm trying to design a guide section in my app using fragments. I'm using 6 fragments in my GuidActivity.java which related to its activity activity_guid.xml , the activity contains coordenator layout which contains an image of dotsat the bottom, I want when I navigate into next fragment the image get changed to another image (the next image has yellow second dot ) and so on , any help or more simple idea to preview the progress of fragments ??
Use a ViewPager instead, you will have gesture support and onPageChanged triggered when you scroll to next page.
A simple Onboarding screen doesn't need to use fragments

Swipeable tabs and programmatically adding them

At the moment, I have a ListView filled with data and when an item is selected, a new activity is opened.
I want to change this so that when an item is selected, a new tab should be added with a certain title and the activity showed underneath that tab, I also want the user to be able to swipe between tabs.
After looking on the android development website, I found https://developer.android.com/design/building-blocks/tabs.html#scrollable which shows an example of the Play Store app and the scrollable tabs they have there. However, I cannot find any examples of this would be set up and also I could programmatically add tabs.
TheNewBoston If you are lost, you may want to go a few tutorials back to 84 in this list
On the side note, have you consider fragments and a viewpager?

How to put tabs containing ListFragment on the left and a details panel on the right?

I have to show two tabs each containing ListFragment classes. On clicking any item of the list, it's details should open on the right panel on landscape views. This is much like the official Android fragments example.
What I want to achieve is that on the left side of the layout the list view should be in tabs, i.e. two list views within tabs. On clicking any item the details should open on the right. Till now, I can show tabs and details, but the details are not showing up on the right. They open as a new activity.
Tab Navigation can be an option, but due to some design restraints in my app, I can't use that. Please guide.
Please check this android blog http://android-developers.blogspot.com/2011/02/android-30-fragments-api.html It has a complete tutorial of how to use fragments as a Master-Details View.
I came to a solution. I am explaining how I did it, in case someone needs it.
I wanted to make tabs on the left fragment. Due to design constraints, I could not use Tab Navigation, as I had to use List Navigation too.
So, I made a new fragment placed over the left fragment and inflated it with two buttons. On different button clicks, I used FragmentTransaction to add new fragment to the left fragment.
On button click listener I used fragmentTransaction.replace method.

Tabs disappears in TabActivity after it goes to other Activities

In my app I have four tabs in the bottom of my main screen. When the user click in one of the tabs in the bottom it open an Activity and the user needs to fill up a form, after the form is done it returns to one of the TabActivities.
Everything works well, except when the form is save its supposed to go to the Activity an show the four tabs in the bottom. The problem is that the tabs in the bottom disappear.
I thought the if Im calling the TabActivity it will always show the tabs that were assign in the XML layout.
Okay you have to go for ActivityGroups. But in later version of android Activity Group is deprecated and hence you have to go for fragments or views. Here are few links which will get you started.
ActivityGroup Example
http://blog.henriklarsentoft.com/2010/07/android-tabactivity-nested-activities/
http://united-coders.com/nico-heid/use-android-activitygroup-within-tabhost-to-show-different-activity

Categories

Resources