Fragments overlapping when I am using fragmentTransaction.add method - android

I am facing some weird issue with fragments, I am displaying google map on these two fragments, while switching from Home Fragment to Request cab fragment, in Request cab fragment, fragments are overlapping, I can see Home Fragment in background and its also clickable I can move map of Home Fragment too. This issue occur when I use fragmentTransaction.add(R.id.frame, fragment, fragmenttag);
but if I use replace then its working fine, but I want to get the view back onBackPressed that is not possible with replacing the fragmnet. I am attaching screenshots and code.
public void changeFragment(final Fragment fragment, final String fragmenttag) {
try {
drawer_close();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction().addToBackStack(null);
fragmentTransaction.add(R.id.frame, fragment, fragmenttag);
fragmentTransaction.commit();
} catch (Exception e) {
}
}

You just need to add background when you add fragment.
add(): adds the new fragment on the top of another fragment that's why you can see below fragment
replace(): removes everything then adds the new fragment

add and replace work quite differently, while replace removes current fragment from inflated view group. add method just adds new layout inside of ViewGroup. thats why you can see your fragment and thats why its clickable just think them as normal views and not fragments. now you could just add white background to root layout of second fragment and make it clickable it should solve all your problems.

Related

how to restore last opened fragment inside an activity after getting back from another actvivty

I'm using multiple fragments inside an activity like this flow: Fragment A has a list on it's item click Fragment B opens and it also has a list opens Fragment C which has a list Open another Activity , The problem is when I go back from the other Activity I found Fragment A is opened, How I restore the last Fragment C when go back from the other activity?
here is the code of replacing Fragment inside my activity
protected void showFragment(Fragment fragment) {
String TAG = fragment.getClass().getSimpleName();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.container_home, fragment, TAG);
fragmentTransaction.addToBackStack(TAG);
fragmentTransaction.commit();
}
replace removes the existing fragment and adds a new fragment. This means when you press back button the fragment that got replaced will be created with its onCreateView being invoked. Whereas add retains the existing fragments and adds a new fragment that means existing fragment will be active.
Use add instead of replace
fragmentTransaction.add(R.id.container_home, fragment, TAG);
I had the same issue solved using the above code.
You can use
fragmentTransaction.addToBackStack(null);
instead of,
fragmentTransaction.addToBackStack(TAG);
Hope Your problem will solve.

Activity Buttons still ACTIVE when replaced by Fragment

Basically, I want to open a fragment from Activity. Everything worked fine (activity button opens the fragment and the fragment shows), until I miss-clicked outside the fragment's layout where one of the Activity button is located and it did its onClick method. That's why I wondered why is my Activity still active.
this is how I open the fragment from Activity
I tried using .add instead of .replace still the same.
#OnClick(R.id.login_register_lbl)
public void registerAction(View view) {
// TODO register transition...
FragmentManager fragmentManager = (LoginActivity.this.getFragmentManager());
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
RegisterFragment fragment = new RegisterFragment();
fragmentTransaction.replace(R.id.fragment_register_holder, fragment);
fragmentTransaction.commit();
}
In fragment layout in root element set "background" , "clickable=true" & Focusable="true".
In my opinion you can achieve this functionality by 2 ways:
Open a dialog fragment in full screen mode to avoid accidentally clicks on activities views.
If you wanted to show both fragment's and activity's views at the same time then, on click of your activity's button which opens the fragment, disable it by yourbutton.setenabled(false); and when you wanted to close the fragment enable that button again.

Android toolbar showing images of previously added fragment

I used toolbars in fragments instead of inflating it to activity.I am using different ImageViews in each toolbar layout.I was working fine. When I changed fragment transaction replace by add it is showing images of current layout images and previous fragment's images also.transaction.add(R.id.fragment_container, newFragment);What can I do?
I replaced replace by add since it showing some transparency while animating the second fragment from bottom.
This is how I added second fragment,
Fragment newFragment = new DetailsFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.setCustomAnimations(R.anim.slide_in_up, R.anim.splashfadeout);
transaction.add(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
transaction.commit();
if You use image different image in different fragment then you must hide or remove resourse onDestroy like it.
#Override
public void onDestroy() {
super.onDestroy();
MainActivity activity = (MainActivity) getActivity();
activity.mToolbarRightImageView.setVisibility(View.GONE);
}
if in which fragment you not use that ToolBarimage then on create of fragment you also hide that image.I faced this problem but i solved this way
MainActivity activity = (MainActivity) getActivity();
activity.mToolbarRightImageView.setVisibility(View.GONE);
If You want to use toolBar ImageView in each fragment , You make public in Mainactivty then you use above instruction

FragmentTransaction add() behavior

Working with fragments I've always used replace() for my transactions, but I wish I didn't have to save instance states anymore to restore a fragment's view and prevent reloading when coming back to that fragment. So, I've decided to work with add(). The thing is when I add another fragment, the previous fragment view remains in the background and that's fine (that's the behavior I expected), but the problem is I can actually interact with the views in the background. Example:
Fragment A has a Button
Fragment B has a TextView
When I add Fragment A and later add Fragment B, I'm able to click on Fragment A's Button, even staying on Fragment B's view.
I'm using:
FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction().
add(getRootViewContainer(),fragment,fragment.getClass().getSimpleName());
if (shouldGoBack)
fragmentTransaction.addToBackStack(fragment.getClass().getSimpleName());
where getRootViewContainer() returns the id of the FrameLayout I'm using as my activity main container.
Now, is it really the default behavior of add()?
If so, is there a proper way to avoid this or one just has to use replace()?
What you can do here is just hide previous fragment at the time of transaction of current fragment.
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
Fragment newFragment= new MyFragment ();
ft.hide(CurrentFragment.this);
ft.show(newFragment);
ft.commit();
It worked for me just try it.
FragmentTransaction.hide(fragmentBehind); //works for me!
example :
//I have it globally available
FragmentTransaction trans = MainActivity.getManager().beginTransaction();
//not globally
FragmentTransaction trans = getFragmentManager().beginTransaction();
MapFragment newFragment = new newFragment();
trans.add(R.id.fragmentContainer, newFragment, tag);
trans.hide(this);
trans.addToBackStack(tag);
trans.commit();
Yes, this is a default behaviour of add().
If you really don't want to user replace(), you can try to disable views which are inside "old" fragment.

android:onclick="" - to call a layout

I'm wondering if it would be possible, to use the XML-onclick attribtue to call another XML-"View".
Means if I'm in a menu and click on the button "Create new Drawing", shouldn't I be able to call just another XML-View to bring up more options?
What I know is: android:onclick="hellothere" calls the method public void hellothere(View view) in the Activity to whom the view belongs, but why does it has to be used with code, instead that I'd just call another XML-Layout?
Example: andorid:onclick="new_page" (new_page.xml opens)
Calling another xml without a context is not possible in android. The context would be something like an activity, fragment etc.
If you want to inflate another xml into an area on your desired layout without activity transition, use fragments instead.
Heres's a simple code how to attach a fragment in a specific or generic area:
attaching to generic fragment area:
FragmentTransaction ft;
Fragment mFragment = new MySampleFragment();
ft = getFragmentManager().beginTransaction();
ft.replace(android.R.id.content, mFragment, "samplefragment");
ft.commit();
as you can see above, we attach the Fragment into the default fragment inflater of android which is "android.R.id.content".It gives you the root element of a view, without having to know its actual name/type/ID. Check out Get root view from current activity
attaching to specific fragment area:
FragmentTransaction ft;
Fragment mFragment = new MySampleFragment();
currentFragment = mFragment;
ft = getFragmentManager().beginTransaction();
ft.replace(R.id.myfragmentid, mFragment, "samplefragment");
ft.commit();
Here we inflated the fragment inside a specific area with an actual ID which is R.id.myfragmentid. In this case you can specify which area in your page you will show your desired output upon a specific events like button click, hover, etc.
Hope it helps. Cheers! :)

Categories

Resources