I have a dialog fragment(FragmentA), with a frame layout.
I am trying to add another fragment(FragmentB) into the frame layout.
public void setFragment(Fragment fragment) {
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.replace(R.id.frame_canvas, fragment);
transaction.commit();
}
Just to confirm, I am logging inside OnCreateView of FragmentB, and it gets printed when call setFragment.
But the view of FragmentA doesn't add FragmentB into the frame layout.
Edit:
FragmentA is actually a DialogFragment. Can that be the issue?
Use this I think it will works.
If you on Fragment A than use code like below
Fragment fr = new FragmentB();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frame_layout, fr);
fragmentTransaction.commit();
if you are on activity from where you call fragment B than use Code like below `
Fragment fr = new FragmentB();
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.frame_layout, fr);
ft.commit();`
// xml
<FrameLayout
android:id="#+id/frame_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/general_home_toolbar">
Related
I have tried
YourFragment mapFragment = new YourFragment();
FragmentManager manager = getFragmentManager();
manager.beginTransaction().replace(R.id.landlord_apartment, mapFragment, mapFragment.getTag()).commit();
But my acivity overlapped to my fragment?
How can I solve that?
I want to back via action bar arrow button
please try this solution if you back to fragment from fragment
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
YourFragment yourFragment = new YourFragment();
fragmentTransaction.replace(R.id.fragment_container, yourFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
but from activity to fragment just add
finish();
I have coded like this:
FragA >> FragB >> FragC >> FragD
When I press onBackpress() of FragD it goes to directly on FragA but i want to keep it go FragC.
BackPress Code here :
if (getActivity().getSupportFragmentManager().getBackStackEntryCount() > 0) {
back_btn.setVisibility(View.GONE);
getActivity().getSupportFragmentManager().popBackStack();
}
Replacing Fragment Code :
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
FragmentA fragmentA = new FragmentA();
fragmentTransaction.replace(R.id.framelayoutinner, fragmentA);
fragmentTransaction.addToBackStack("fragmentA");
fragmentTransaction.commit();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
FragmentA fragmentB = new Fragmentb();
fragmentTransaction.replace(R.id.framelayoutinner_2, fragmentB);
fragmentTransaction.addToBackStack("fragmentB");
fragmentTransaction.commit();
You are using nested fragments, if you load fragment from another fragment then it become nested structure. So try to check if any child fragment exist within your Root fragment then pop that child fragment first.
Use getChildFragmentManager() for that and make recursive calls till you get most young child fragment(Last added). Or better use Tag on your fragments.
You need to add every fragment into backstack using addToBackStack method
public void setmFragmentContainer(Fragment fragment)
{
final String tag = fragment.getClass().getName();
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.layout_content, fragment, tag);
transaction.addToBackStack(tag);
transaction.commit();
}
This method may help you
How to refresh or rebuild a class that is extended from RowsFragment?
I have tried a few codes:
RowsFragment frg = this;
frg.getFragmentManager();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(frg);
ft.attach(frg);
ft.commit();
or
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(this).attach(this).commit();
but still nothing.
You are simply using same instance while reattaching the fragment.
Create new instance of the fragment and replace it with existing one.
RowsFragment fragment = new RowsFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.container, fragment); // use your container in the place of R.id.container
fragmentTransaction.commit();
In Fragment3 have child fragment, if i open Fragment3 from Fragment1 then Fragment4 is shown but now i open fragment3 from fragment2 then fragment 4 is not shown.
And vice-versa if i open Fragment 3 from Fragment 2 then Fragment4 is shown, but if i open Fragment 3 from Fragment1 then Fragment 4 is not shown.
Code to add Fragment 4:
Fixture_H2h_frag fragment1=new Fixture_H2h_frag();
Bundle b1 = new Bundle();
b1.putBoolean("Show", true);
//b.putString("RESULT", result);
b1.putString("URL", Url);
b1.putString("TYPE", "FORM");
b1.putString("VisitorTeam1", matchlist.get(pos).getVisitor_image_url());
b1.putString("LocalTeam1", matchlist.get(pos).getLocal_image_url());
fragment1.setArguments(b1);
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.Inner_Fixture_Container, fragment1);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
Code to add Parent Fragment:
FixtureDescrption_Frag frag = new FixtureDescrption_Frag();
FragmentManager fm = contxt.getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.add(R.id.fragment_place, frag);
fragmentTransaction
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
if(addedtostack){
fragmentTransaction.addToBackStack(contxt.getClass().getName());
}
fragmentTransaction.commit();
You need to use
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
instead of
FragmentTransaction transaction = getFragmentManager().beginTransaction();
While adding child fragment.
You can refer this link for more details.
I am developing a simple android app only for tablets and using android 4.0. My application have the main screen like as follow:
Oncreate() of Main Activity I am adding Fragment A in the main.xml using following code:
FragmentTransaction ft = getFragmentManager().beginTransaction();
Fragment imageFragment = new ImageFragment();
ft.replace(R.id.fragment_container, imageFragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(null);
ft.commit();
This fragment A just have only a Image view which is clickable. Now I want that when user click on Image view then another fragment (Fragment B) should call and it replace the image view. The Fragment B have a VideoView which play the video.
So My second screen should be like as follow:
My problem is I am not gettting "How to call second fragment from the first one with in main screen activity?"
I can use different activities but I do not want to do so and just want to run this using fragments.
Please guide me.
This is the simplest way:
1) Inside YourActivitycreate a method:
public void goToSecondFragment(){}
FragmentTransaction ft = getFragmentManager().beginTransaction();
Fragment secondFragment = new SecondFragment();
ft.replace(R.id.fragment_container, secondFragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(null);
ft.commit();
}
2) In your first fragment, when you want to replace it, call:
YourActivity activity = (YourActivity) getActivity();
activity.goToSecondFragment();
You can do the same using below:
Adding a fragment with maintaining a back stack
FragmentManager supportFragmentManager = getSupportFragmentManager();
supportFragmentManager.addOnBackStackChangedListener(new OnBackStackChangedListener() {
#Override
public void onBackStackChanged() {
}
});
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
fragmentTransaction.addToBackStack("fragmentTag");
fragmentTransaction.replace(R.id.slate, fragment, "fragmentTag");
fragmentTransaction.commit();
And Replacing a fragment
FragmentManager supportFragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
fragmentTransaction.replace(R.id.slate, fragment, "fragmentTag");
fragmentTransaction.commit();
Should work for you.