I am trying to implement bottom sheet behavior to fragment.The two points are need to implents
i)calling one fragment to another when button is clicked.
ii)apply bottomsheet behavior to fragment B,So that When I drag down the fragment can hide/dismiss.
Currently I am having two fragment A,B.I call fragment B in Fragment A by a container When button is clicked .
But when I am trying to implent second point I get error.On FrgmentB I am extend only Fragment not Fragmentdialog. The main reason is I cannot able to acess the background UI.
I have the same problem before, what I did is change the fragment to BottomSheetDialogFragment and made some changes.
Related
I have an container activity which will contain the fragments which will be added to this container when my bottom nav bar is navigating. Now I want to go to one of this fragment to another fragment which will replace another different activity by set on click listener of a cardview.
I am new fragment so I dont understand how to do that. Hope someone will help me.
You approach is wrong. You cannot navigate from one activity fragment to another activity fragment. The correct way will be to have single activity with multiple fragments inside. Using Navigation component will be the best way of navigation.
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.
I have 2 fragments.
I want to drag a view from fragment one and drop it to another view in fragment two.
What i do now is i start dragging (using drag and drop api) inside fragment one , then i hide fragment one then i show fragment two which has a drag listener event on some view.
I use fragment transactions of add and show and hide to make sure that the fragments are not destroyed or recreated , i never use replace for transaction.
However the onDrag method in drag listener interface inside fragment two is never called when i hide fragment one and show fragment two.
I have done something similar before that worked , except that fragment two was overlapping fragment one and both appeared on UI , and when i start dragging inside fragment one i only hided it then the drag listener inside fragment two was active .
So the difference in the case that worked was that i only hided fragment one by transaction.
However in the case which is not working i hide fragment one and show fragment two by transaction.
Don't know why are the results different.
For further details on my case i'm using BottomNavigationView and it has 3 fragments , i'm trying to drag and drop between 2 of those fragments.
I managed to solve this by sending the view object via event bus to fragment two ( after the user in my case long pressed on the view in fragment one ).
Then i switched fragments as mentioned in the question.
Then i started using the drag and drop api from fragment two event bus receiver that contains my view object.
I have this layout that Half of the screen is framelayout and the other half is buttons.
The framelayout has a fragment.
And my buttons on the other half has some commands that will change the views of the fragment.
But my problem is, How would I know if the button is clicked which is inside the activity to tell the fragment that button is clicked.
I cant put the button inside the fragment because the button will be used on other fragment. If there is only a way.
The easiest way is using interfaces. Define an interface inside your fragment, and let the activity implement that interface.
Check this link: https://developer.android.com/training/basics/fragments/communicating.html
In my application I am making use of navigation drawer which is in the MainActivity and this navigation drawer has say 5 fragments. I am not maintaining any backstack of these fragments.
Now, the first fragment has one button which when clicked pushes a fragment (which I call an inner fragment). Here, I am maintaining a backstack because I want to get back to the first fragment from the inner fragment.
Now, I have a requirement in which I want to navigate from an activityA to the inner fragment.
Is this possible?
One way that I have thought of is to have the push code inside the first fragments create method (and make this conditional).
But I don't think its an appropriate way. Any suggestions would be helpful.