Fragment hierarchy changes after rotation - android

I have an activity which contains 2 fragments. Fragment 1 is a listview and fragment 2 is a normal view which is presented when an item in the listview is tapped. Consequently, fragment 2 should always be above fragment 1. Most of the time when the device is rotated, the fragments are restored in the proper order. However sometimes fragment 1 is restored above fragment 2 - what could be the cause of this and how can I fix it?

Could be that the z-ordering has been messed by reloading.
Have you tried Views bringToFront() Method?
http://developer.android.com/reference/android/view/View.html#bringToFront()

Related

Parent fragment destroyed with shared element transition

I have a view hierarchy such as below.
MainActivity :
FirstFragment :
SecondFragment :
In my SecondFragment, i have a RecyclerView. When an item is clicked, i want to open it in another Fragment (in full screen) so i am using the addSharedElement() method and everything works fine after 2 days of retro engineering it : every post i have found on StackOverFlow does not mention that in order to work, the "full-screen" fragment has to replace the first parent inside the activity.
What i first wanted was to nested the full-screen fragment inside the SecondFragment but it's impossible with this method.
I have tried everything else, at each steps of my view hierarchy & none of them is working, except if i replace my FirstFragment by my full-screen fragment.
This is also impossible to use the transition with the add() method unfortunately.
The problem is that this fragment is destroyed & not recreated despite the call of addToBackStack(), when i press the "back-button" inside the NavigationBar.
I am not really familiar with the addToBackStack() method & how can i get back any fragments i have added there.
QUESTION 1 : How can i make it work either without destroying the parent fragment (FirstFragment in my view hierarchy), or without using shared element transition but achieving the same result ?
It seems that what the Transition is doing is recreating a view in the new fragment with the same width / height, x & y coordinates as the one in the previous fragment & animate it from the new fragment...
QUESTION 2 : Would it be a bad idea to do it myself ?
EDIT : My goal is to close the full-screen fragment with a swipe down and as soon as the finger is moved from 10dp along the Y axis, i want to be able to reveal the previous fragment behind (the SecondFragment in my case), that's why i don't want to destroy parent fragments.

Fragment initialized without onCreate being called

So I have a fragment inside a viewpager which is contained inside a fragment which is getting initialize'd as shown in the debugger but doesn't have it's onCreate,onCreateView or any such methods being called. The activity containing has a bottom navigation view and contains 4 such fragments and this issue is only happening in 1 such fragment.
All these fragments and viewpagers and fragments inside them are created on the oncreate of the activity. If I move the logic to create the fragment to when a bottom tab is clicked, this issue gets solved on.
How is this possible ?
Only the currently active fragment and the two directly adjecant fragments are created when the viewpager is first shown. so the fragment for the first page, the fragment for the page shown if you "scroll to the left" and the fragment for the page shown when you "scroll to the right". the 4th fragment will be created when it is put in the next adjecant position.
so if you have fragments a,b,c,d in a viewpager like -a-[b]-c-d- where b is the first visible page, only a,b and c will be created at startup. when you scroll to c -a-b-[c]-d- onCreate for fragment d will be called.
You can control number of pages/fragments preloaded when you launch your activity by setting it's off Screen Page Limit on your view pager adapter. like this
mViewPager.setOffScreenPageLimit(limit)
If you set limit to 0 than only first fragment would be load.
https://spotandroid.com/2016/11/23/android-tricks-viewpager-and-offscreen-tabs/
https://techcodegeek.wordpress.com/2015/06/23/android-viewpager-and-performance-improvements/
Need more detail to know your issue.Issue which you are facing is not understood with information you have provided.

How to save some value and destroy a fragment after swiping?

I have several fragments. I want them to come one after another as ViewPager. If i swipe right then a fragment will be destroyed storing a value "yes" and if i swipe left it will destroy storing a value "no" and next fragment will appear. I tried FragmentStatePagerAdapter but not getting desired result. I have google a lot but unable to do as I want.
Suppose I have 3 fragments 1,2,3.
1 is the first fragment. If I swipe right/left fragment 1 will destroy storing a value and fragment 2 will appear, and so on. When swiping to last fragment, a Dialog box will pop up with completion message.
Your ViewPager does not destroy your fragment, when you swipe from fragmet 1 to fragment 2. However, if you swipe from fragment 2 to fragment 3, fragment 1 is destroyed. This also applies to when you swipe from fragment 2 to fragment 1. In that case, fragment 3 is destroyed.
If you want to store a value between swipes, you should probably use the onPause callback method of the respective fragment.

Strange fragment behaviour - empty container in view hierarchy after screen rotation

I encountered very strange problem with fragments. In my case I have fragment which is placed inside activity. It has two type of layout:
layout - with Frame Layout with list_container id
layout-land - with two Layout: list_container and detail_container.
When user enters activity in portrait mode he sees TestListFragment. When he clicks one of the list element TestDetailFragment is added to backstack.
Next, when user rotates screen TestDetailFragment is removed from backstack and placed inside layout with id detail_container. How strange part appears, when inspecting view hierarchy there are two list_containers (one is empty and is over the rest of the content causing view overlap). onActivityCreated in TestFragment is triggered twice.
I'm including hierarchy view and screens:
http://i.imgur.com/cw37VX4.png
http://i.imgur.com/xQYalCJ.png
http://i.imgur.com/SObusj0.png
http://i.imgur.com/2sVhlIP.png
http://i.imgur.com/BCXrQ1K.png
Source code:
Activity with fragments: http://pastebin.com/2vAqQYgw
Activity layout: http://pastebin.com/iEawVBCx
TestFragment layout: http://pastebin.com/z5MZ5eR9
TestFragment layout-land: http://pastebin.com/RJHYRinM
Is this normal behavior? How to get rid of this layout?
When you nest fragments (i.e., have a fragment host a fragment), the child fragments need to be set up either by:
having the parent fragment inflate a layout containing <fragment> tags
having the parent fragment use getChildFragmentManager() when executing a FragmentTransaction to add the child fragments
In this case, the code in the question is using getActivity().getSupportFragmentManager(), not getChildFragmentManager().
To be honest, I cannot completely understand how this resulted in the cited symptoms, but according to the comments on the question, it helped.
When Android detect rotation it destroyes the running activity and create a new one, so if you have a behaviour on onCreate() and onDestroy() of the activity, keep in mind that. Take care also for Fragment that has a further event to manage and is related on the attached Activity onAttach()

FragmentStatePagerAdapter performance issue

I have implemented an activity that displays fragments in a viewpager. The activity initially displays 5 fragments of a given type (type A). Each type A fragment contains a list of information. When a button is pressed, the fragments are replaced with another tpye of fragments, type B (i.e., fragments that use a different layout and display different information are loaded).
I have implemented this with a ViewPager and a custom FragmentStatePagerAdapter. The custom FragmentStatePagerAdapter overrides the getCount, instantiateItem, getItem, getItemPosition, etc. functions and all works fine. When I press the button the views switch from fragment type A to fragment type B. In order to do this I am using a replace fragment transaction and I also call notifyDataSetChanged on the adapter. The switch from type A to type B fragments is accomplished by checking which type of fragment I need to create in the adapter's getItem function. The problem is that the switching process (i.e., changing from fragment type A to fragment type B and vice versa) takes some time (around 2 seconds) - this delay is quite noticeable and annoying.
This occurs because the adapter's getItemPosition function returns POSITION_NONE. When POSITION_NONE is returned, the viewPager recreates the fragment. Furthermore, when the replace transaction is executed, all items of the adapter are removed and all new ones are recreated. The same happens when I switch back to fragments type A.
Does anyone know if there is a better or faster way to do this? Using a FragmentPagerAdapter should not be a good solution since the number of fragments in the viewPager could grow.
I have commented out the updating of the fragments and the problem is gone so the problem is the time it takes to update the fragments. Is there a way to update the fragments asynchronously? That should take care of the problem.
The ViewPager instatiates by default a minimun count of Fragments. In general the left and right one of the current Fragment. So you don't need to care about the total number of Fragments.
You can control the total number with the ViewPager.setOffScreenPageLimit() method.
Also a AsyncTask seems to be a good idea to manage the intensiv work of your Fragments.

Categories

Resources