onBackPressed and Fragments - android

PROBLEM
I've MainActivity with onBackPressed overridden that holds SearchFragment within and FrameLayout for DetailsFragment(its an XML for Master/Detail 600dp-land).
Normally the DetailsFragment appears in its own Activity but on tablets its in FrameLayout.
This DetailsFragment have additional View that is normally hidden but might be shown via user interaction.
What I need is determine first whether View must start animating(closing) or leave Fragment.
I want to achieve closing this additional View before leaving the DetailsFragment with onBackPressed.
I have no idea how to solve this problem!
Please help me.

Related

What will be fragment behavior when came from background. Considering fragment is in stack

I have a Navigation panel activity. With 5 fragments (Will name it as Fragment1, Fragment2, ...) in menu sections.
Now by Default, activity will display Fragment1.
If user navigate to Fragmentxtz from Fragment1. We will add the fragment on top of Fragment1.
Now user goes to background by pressing home button and open the app from tasks.
Now i know Fragmentxtz onStart will be called. But i see that Fragment1 onStart is also called.
Is this expected behavior ?
As you can see on Android Developers, your fragment will be called at onViewCreated().
https://developer.android.com/guide/components/fragments#Creating
The View has to be updated in case you changed the system language or something like that.
Providing a bit more context to what might be happening here.
If both of your fragments are added e.g. via FragmentTransaction.add() both of them will have onCreateView() called when the layout is restored for the user as Robert pointed out. From the system's point of view all of those fragments are relevant to the user and would be shown simultaneously.
If on the other hand you add fragments via FragmentTransaction.replace() only the topmost fragment on the back stack will receive the onCreateView() call. This can also be achieved by doing an add and remove of the old fragment. If you make this transaction reversible by the back stack after pressing the back button your previous fragment would receive the appropriate lifecycle callbacks.

Fragment replace makes root layout visible. How to fix?

I am experiencing the following issue:
I have a single activity and a fragment container inside this activity layout. After user presses a button from the single activity root layout I call:
supportFragmentManager.beginTransaction()
.setCustomAnimations(R.anim.custom_fade_in, R.anim.custom_fade_out)
.replace(R.id.single_activity_container, firstFragment, FIRST_FRAGMENT)
.commitNow()
It works well. However, if I need to add another fragment on top of existing I use same code block. The issue is that previous fragment gets removed and user can see root view for a split second. This is really a bad UX and I found no way to solve it. I need smooth transition, without revealing root layout again.
I specificcally need to use replace in order to trigger Fragments onPause and onResume, because calling add(), does not trigger lifecycle events.
How should I structure my presentation logic to fix this UX issue?

Should a fragment be able to change the State of a View inside the host Activity?

If I need to hide a view inside the Activity when a certain fragment is inflated, is it ok to let the Fragment do the State Change?
For example I have a three Fragments (FragmentA, FragmentB, FragmentC) and one Activty. The Activity have a BottomNavigation View but its visibility should be set to Gone if FragmentB is inflated inside the Activity.
If I placed the managing of the BottomNavigation Visibility inside the fragment then, I am sure that whenever that fragment is inflated the view will certainly be set to gone.
My only problem is that, if there come a time that I need to reuse that fragment and show the BottomNavigation at the same time. I wont be able to do so because the Fragment will automatically set the Visibility of the BottomNavigation to Gone.
Can anyone give me some tips? Thanks in advance.
In your case, do not control the visibility of the BottomNavigation inside the Fragment, do it inside the Activity with a callback.
read about callback this in part "Creating event callbacks to the activity"
Fragments should be self-sufficient and should not know anything about other Fragments and Activites.

Navigate up to last shown fragment

I've implemented a NavigationDrawer within the MainActivity which containts two Fragment. The second Fragment contains a ListView that opens a new Activity (onItemClick) that shows the according detailed data. (I guess that's pretty much the master/detail flow).
When I use the up button to navigate back I got shown the first fragment and not the second fragment with the ListView.
Any ideas how to solve that problem?
Make method in MainActivity for example setFragment(int whichFragment);
and set fragment you want in it, you should already have code that do that and than call that method in onBackPressed() method.
For your question about another fragment, well it depends on how your master/detail flow is suppose to work, it is not a problem to use another activity if you dont need left menu any more, but if you do need left menu then use another fragment.
Best regards

Fragment LIfecycle in Custom Tab View

I have a custom widget that performs FragmentTransaction.replace when buttons are pressed. Currently, my code is set up such that the first time a fragment is created, it attaches a bunch of stuff to the view that isn't originally part of the xml layout file.
When the app first launches, all my fragments show stuff correctly, however, let's say I start on Fragment A. I can then transition to Fragment B (with B showing up correctly), however, when I transition back to Fragment A, all the stuff I have attached to the view of Fragment A is now gone. I know this happens because onCreateView is called which probably means the Fragment's view is re-generated when FragmentTransaction.replace is called.
Is there a way where I can keep my fragments around instead of having them re-generate their views when FragmentTransaction.replace is called?
Thanks!
Instead of using fragmentTransaction.replace, use fragmentTransaction.show and fragmentTransaction.hide.
That will keep your fragments from being destroyed.

Categories

Resources