I have a fragment, which consists of a viewPager in the top of the screen and two buttons at the bottom.
The buttons are for an additional navigation besides the swiping through the pages.
The viewPager consists of about 10 pages with some input fields.
However, when I swipe through the pages and rotate my phone to landscape, the viewPager automatically changes the current item and selects the first page again. Another effect is, that all input data to this point are cleared.
This is really annoying because you have to start all over again.
Is it possible to switch between landscape and portrait mode without this issues?
Some background information:
the app consists of one activity, which is filled with different fragments
a navigation drawer is implemented to choose which fragment will be shown
When you change orientation you are essentially refreshing your activity losing your data such as current page, fragment etc, you need to save your data across the states, i would suggest looking into savedInstanceState.
Related
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
I am trying to read RSS feed and show the content of it in card view which is inside ViewPager with tab layout. It shows fragment with data initially, but when swiped back from another tab, the whole card layout with data,disappears.
I had to handle the same situation. I had TabLayout in ViewPager.
I have used FragmentPagerAdapter with off screen page limit set to (NO_OF_TABS - 1). This is to hold all the fragments in memory and they wont be destroyed and recreated as you swipe back and forth. As my fragments are small in size meaning not many views every fragment has so it was okay for me. But this way fragments always holds the latest data. Say, if user had entered text in EditText and swipe to end and come back, then fragment will still have the text that user had entered.
In your case, if you have not set the page limit the default is 1 so the fragment gets destroyed when you swipe away so you lose the data.
IN ADDITION TO ABOVE:
If you can not keep all fragments in memory, you may consider using FragmentStatePagerAdapter. This adapter saves the states of fragments that are being destroyed and it will use the same state to represent in fragment when user swipe back to this fragment. But there are issues around StaePagerAdapter while it destroys adn recreate the fragments, there are indexing issues so you will easily get IndexOutOfBoundException if you swipe. This is with page screen limit set to less than the total fragments.
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.
My application has a menu within it that is divided into two panes when the screen is in landscape. Essentially it looks like below:
I also have a second layout, where the two fragments are separate, and clicking one item int he list opens the fragment of for the product that was clicked, like so:
The question is, what should happen when the user switches orientation? Going from portait to landscape is easy, but what about going the other way? If the user has clicked on a product (thus filling the right fragment) should they be taken back to left or right fragment on orientation change? There are spots in the app where the user can bypass the list and be taken directly to product; what happens if a user does that and changes orientation?
So my question is more best practices in design. Is there a standard best practice or rule of thumb for deciding which fragment stays on screen during an orientation change?
This is a repeatation of a post Recreating off screen pages of ViewPager while changing orientation as i dont get any response and i am in a big trouble.Moreover i am repeating the whole text here.
Problem:
From internet i came to know that Android ViewPager needs to set at least 1 offscreen page limit for both side with viewPager.setOffscreenPageLimit(1), in that case when i am watching a page, two more pages in both side of the middle page have already been created. Now the problem is when i am going to Landscape mode and swiping some page and then turn it back to the Portrait mode, only the middle page is being recreated (not the offscreen pages). May be that is why a shadow/marginal portion of the previous page (that was created with landscape margins) is always shown overlapped with the middle page (that is recreated with portrait margins).
Now how to get ride of this problem. I don't want to see this overlapped portion of the previous page. I have attached an image just after going from Landscape mode to Portrait mode.
You can try instantiate adapter with getChildFragmentManager() instead of getFragmentManager() or getSupprtFragmentManager(). I had a similar problem and all I did was it...
Please use getChildFragmentManager() when using viewpager inside Fragments and getSupportManager() when using pager in activities as source for pager adapter