Working with fragment above Main Activity - android

Im working with fragments on my main activity I have a Edit Text and buttons in my main activity But when I opens fragment page and click exact at the spot of Edit Text the keyboard activates.
So it means the below activity is also in working at same time

Fragments are designed to be inside an activity, if you don't want to make your activity visible you can use a full screen layout for fragment, to remove the clicks you can replace .add command with .replace when transacting to fragments give a background color to the root view of fragment layout also. I think this would help
YourFragment fragment = new YourFragment();
getSupportFragmentManager().beginTransaction()
.replace(R.id.ll_job_view_fragment_container, fragment)
.addToBackStack(TAG_FRAGMENT_YOUR_VIEW)
.commit();
Edit
The replace may not help, If you inflate different fragments in the same container you just give the attribute android:clickable="true" to the top most parent view of each fragment that will help.

Related

Android: how to create a custom back button for fragments in a KIOSK app

I CANT use ActionToolbar because this is a KIOSK app with a custom header fragment that needs to be displayed at all time. Hence my situation, I have a basic ImageButton on the said header fragment, and I want to implement the functionality of a basic ActionToolbar.
I add my fragment to the main container like this:
getSupportFragmentManager().beginTransaction()
.replace(R.id.main_container, UserListFragment.newInstance())
.addToBackStack("foo")
.commit();
The question is, how do I restore the state of my main_container before the current fragment was replaced with UserListFragment.
Like : fragment A is showing, replacing fragment A with B, pressing some button in my header fragment to pop the fragment B so again fragment A is showing.
You should consider switching to Android Jetpack Navigation Component it was created for your type of situation.
Check: https://stackoverflow.com/a/62252603/8714139
Cheers!

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.

Why the button in old fragment still working when add the new fragment in android?

I am developing in Android. There are two fragment in an Activity. The fragment-A has a Button , and the Activity will show the fragment-A first.
I use the following code to add the fragment-B.
SecondFragment secondFragment = new SecondFragment ();
mActivity.getSupportFragmentManager().beginTransaction()
.add(R.id.container, secondFragment )
.addToBackStack(secondFragment .getClass().getName()).commit();
But the OnClickListener of button in fragment-A is still working when I touch the position of button after the view already change to the fragment-B.
Did I missing something ? How to solve the problem if I do not use replace or setClickble to false? Thanks in advance.
This happens due to the transparency of events in fragments.
You could add the below attributes to the root view group of the second fragment to avoid passing click event to backstacked fragments
android:clickable="true"
android:focusable="true"

Edittext on background fragment still editable

I have problem with using EditText with several fragments. I have two fragments: FragmentOne and FragmentTwo, and each fragment has EditText.
After adding FragmentTwo above FragmentOne:
android.support.v4.app.FragmentManager fragmentManager =getActivity().getSupportFragmentManager()
fragmentManager.beginTransaction().add(R.id.frame,new FragmentTwo()).addToBackStack(null).commit;
When I press Enter on keyboard, cursor goes to EditText on FragmentOne. How to fix it? Similar problem was when background fragment was still clickable, but I've solved it. What about this problem?
That is becauase, you are not hiding/removing the previous fragment. identify the previousfragment and hide it before you add the new one.
fragmentManager.beginTransaction().add(R.id.frame,new FragmentTwo()).addToBackStack(null).commit;
this should be
fragmentManager.beginTransaction().add(R.id.frame,new FragmentTwo()).hide([current fragment]).addToBackStack(null).commit;
if you gave a name to your fragment, you can find the fragment by
fragmentManager.findFragmentByName("fragname");
I just bumped into this issue as well. I have a parent fragment and I show and hide a child fragment. There is an editText in the parent fragment which ends up in the back of the child fragment. At the time of displaying the child fragment I went into the fragment layout and set its view to gone, and that removed the focus. Of course if I were to hide the child fragment, then the layout would be visible again.

Fragment Transaction inside a fragment with a Tabbed actionbar not working

I want to be able to replace the current fragment which is loaded via the actionbar tabs using a ViewPager with another fragment that is not connected to any tab like this
getSupportFragmentManager().beginTransaction()
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
.replace(id,frag , "VideoEdit")
.commit();
however when I do this it loads the fragment all right but the tabs don't work anymore and everytime I click on a tab it just stays on this fragment. I managed to find this gist
https://gist.github.com/andreynovikov/4619215 but I'm not sure it's the same thing I'm looking for and I don't want to try it out for fear of messing up my existing code with it.
You should avoid transitions like that.
I'll recomend to use FragmentWrapper.
One more fragment that you placed inside Tab it contains only FrameLayout as a container for other fragments. In this fragment, you could use childFragmentManager to add current Fragment and replace it with VideoEdit.

Categories

Resources