I'm very new to fragment and I'm trying to achieve a certain type of objective here.
Consider this scenario : I have three fragments F1, F2 and F3. Now when I move from F1 to F2 there is no problem. Now the issue is, there is a button in F2 which when clicked, replaces F2 with F3. Which means, F3 should 'take the place' of F2 without creating a new item in the backstack. The purpose behind that is, when the back button is pressed when the user is either in F2 or F3, it should redirect back to F1.
I think I can do that however all sort of mess arises when pressing the back button. Once again, what I want is, when either of F2 or F3 is active, the back button should lead me back to F1.
The code which I'm using to go from F1 to F2 is :
FragmentManager fm = getFragmentManager();
Fragment fr = new FragmentTwo();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.add(R.id.fragment_container, fr);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
The code which I'm using to replace F2 with F3 is :
FragmentManager fm = getFragmentManager();
Fragment fr = new FragmentThree();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fr);
fragmentTransaction.commit();
The code which I'm using to replace F3 with F2 is :
FragmentManager fm = getFragmentManager();
Fragment fr = new FragmentTwo();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fr);
fragmentTransaction.commit();
Any suggestions and tips will be really appreciated.
EDIT :
I have uploaded the source of my project here :
https://ufile.io/4e665
Regards
Alright, so this may not be the best approach but for now it solves my problem. I tried this approach when switching from F2 to F3 and back from F3 to F2 and surprisingly it worked (as in the back buttons are functioning as it should) :
FragmentManager fm = getFragmentManager();
Fragment fr = new FragmentThree();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.add(R.id.fragment_container, fr);
fragmentTransaction.addToBackStack(null);
fm.popBackStackImmediate();
fragmentTransaction.commit();
Also, please note that my onBackPressed is implemented like this :
#Override
public void onBackPressed() {
getFragmentManager().executePendingTransactions();
int count = getFragmentManager().getBackStackEntryCount();
System.out.println("getBackStackEntryCount() VALUE : " + count);
if (count == 0) {
System.out.println("onBackPressed() CALLED");
super.onBackPressed();
//additional code
} else {
System.out.println("popBackStack() CALLED");
getFragmentManager().popBackStack();
}
}
#Override
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() > 1)
getFragmentManager().popBackStackImmediate();
else super.onBackPressed();
}
From activity to F1
FragmentManager fm = getFragmentManager();
Fragment fr = new FragmentOne();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.add(R.id.fragment_container, fr);
fragmentTransaction.commit();
From F1 to F2
FragmentManager fm = getFragmentManager();
Fragment fr = new FragmentTwo();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fr);
fragmentTransaction.commit();
Between other fragments
FragmentManager fm = getFragmentManager();
Fragment fr = new FragmentThree();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fr);
fragmentTransaction.commit();
FragmentManager fm = getFragmentManager();
Fragment fr = new FragmentTwo();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fr);
fragmentTransaction.commit();
Related
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">
I have three fragments a, b, c
I replace a with main activity
FragmentManager fm = getFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.add(R.id.fram_container, new Fragment1(), "tag1");
transaction.commit();
a to b
FragmentManager fm = getFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.replace(R.id.fram_container, new Fragment2(), "tag2");
transaction.commit();
and b to c
FragmentManager fm = getFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.replace(R.id.fram_container, new Fragment3(), "tag3");
transaction.commit();
in c...
I want c back pressed to redirect with a without anything doing in c
I want skip b in back pressed.
What can I do and how?
Implement the below code back press
public void backToFragment(final Fragment fragment) {
// go back to something that was added to the backstack
getSupportFragmentManager().popBackStackImmediate(
fragment.getClass().getName(), 0);
// use 0 or the below constant as flag parameter
// FragmentManager.POP_BACK_STACK_INCLUSIVE);
}
So suppose there are 3 fragments, let's name them F1, F2 and F3. F3 can be called from both F1 and F2 by the following code:
Fragment f3 = new F3();
Bundle args = new Bundle();
args.putString("Id", String.valueOf(id));
f3.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.f1_layout, f3).commit();
I wanna return to where I come from. What can be done? I've played with addToBackStack in different ways but couldn't succeed.
This is how you use addToBackStack() method :
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(..............);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
Try this
fragmentTransaction.replace(R.id.f1_layout,f3).commit().addToBackStack(null);
I have 4 java class that i use as fragments: `F1,F2,F3,F4.
When i want to swich from one to other, i use this code:
android.app.FragmentManager FM = getFragmentManager();
FragmentTransaction FT = FM.beginTransaction();
F1 FP = new F1();
FT.add(R.id.where, FP,"F1");
FT.commit();
FM.executePendingTransactions();
If i want to make possible to return from one of them to the previous, i add addToBackStack(TAG) as following:
android.app.FragmentManager FM = getFragmentManager();
FragmentTransaction FT = FM.beginTransaction();
F1 FP = new F1();
FT.add(R.id.where, FP,"F1");
FT.addToBackStack("F1");
FT.commit();
FM.executePendingTransactions();
I have only one activity that chages the current fragment displayed.
I would get from it, the current fragment displayed.
I tried to write this, but now is always null.
Fragment now=getSupportFragmentManager().findFragmentByTag("F1");
if(now!=null && now.isVisible()) {
//some code for the current fragment
}
Try This it may be help to you
Fragment currentFragment = getFragmentManager().findFragmentById(R.id.where);
if (currentFragment instanceof F1) {
//do your stuff here
}
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.