Android find current fragment - android

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
}

Related

How to replace fragment store first history and delete other transaction?

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);
}

Android fragment navigation via replacable fragment

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();

Redirect the fragment when click on button

I am using this code for replacing the fragment but there is a problem, it's not replacing the old fragment it just override on old fragment so please tell me what is the problem here.
public void selectFrag1(View rootView) {
Fragment frag;
if (rootView == findViewById(R.id.startup1)) {
frag = new S_SignupFragment();
} else {
frag = new F_SignupFragment();
}
FragmentManager fragManager = getSupportFragmentManager();
FragmentTransaction fragTransaction = fragManager.beginTransaction();
fragTransaction.replace(R.id.fragment_signup,frag);
fragTransaction.commit();
}
Are you sure that you pass the parent view, that is going to hold your fragment on the line fragTransaction.replace(R.id.fragment_signup,frag); ?
For fragmentTransaction.replace(...,...) you need to specify a container for your fragment and the fragment itself. Check where does your R.id.fragment_signup directs. More information you could find in this SO question: Unable to replace fragment on button click
fr = new FragmentOne();
fm = getSupportFragmentManager();
fragmentTransaction = fm.beginTransaction();
fragmentTransaction.add(R.id.fragment_place,fr);
fragmentTransaction.commit();
if you use same code then add this before call first button setonclicklistner

hide existing fragment and load new one on same frame

I use the one frame on which changing the fragment views, the problem I have is when I place add instead of replace the new view is placed on the existing one
all I need is it has to replace the existing and place the new one
As I have to reuse the current view when back button is pressed cannot use the replace
Fragment fragment = null;
fragment = new DetailFragment2();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.add(R.id.frame_container2, fragment)
.addToBackStack(null).commit();
If you don't use the support library (which is your actual case), use this code:
final FragmentTransaction ft =
getFragmentManager().beginTransaction();
ft.replace(R.id.frame_container2, new DetailFragment2());
ft.commit();
If one day you'll decide to use the support library, then use this instead (it's only a matter of replacing getFragmentManager() with getSupportFragmentManager()):
final FragmentTransaction ft =
getSupportFragmentManager().beginTransaction();
ft.replace(R.id.frame_container2, new DetailFragment2());
ft.commit();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction;
transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.frame_container2,fragment.newIstance());
transaction.addToBackStack(null);
transaction.commit();
And you need to create newIstance in Fragment class:
public static fragment newIstance(){
fragment frag= new fragment();
return frag;
}

Compare fragments

I've got the next code to switch between 4 fragments in a container [Main, A, B, C].
I need back button to go back to [Main] no matter how the user has navigated through fragments. For example, if I go [Main] >> [A] >> [C] when pressing back should go to [Main].
But I'm not getting the desired result. I think that I'm not doing well the coparisson between fragments.
Launcher MainFragment = new Launcher();
public void switchFragment(Fragment pFragment) {
FragmentManager fm = getSupportFragmentManager();
Fragment currentFragment = fm.findFragmentById(R.id.fragment_container);
if (pFragment == MainFragment){
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, pFragment).commit();
}
else if (currentFragment == MainFragment && pFragment != MainFragment){
//Fragment fr = getSupportFragmentManager().findFragmentById(R.id.fragment_container);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragment_container, pFragment).addToBackStack(null).commit();
}
else {
//Fragment fr = getSupportFragmentManager().findFragmentById(R.id.fragment_container);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragment_container, pFragment).commit();
}
currentFragment = pFragment;
}
UPDATE--
I've seen this way is working better, but still makes issue. If I navigate through some fragments without returning back to the Main fragment, when I press back it doesn't go back. It's like if there was some issue with the popbackstack().
public void switchFragment(Fragment pFragment) {
FragmentManager fm = getSupportFragmentManager();
Fragment currentFragment = fm.findFragmentById(R.id.fragment_container);
if (pFragment.equals(MainFragment)){
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, pFragment).commit();
}
else if (currentFragment.equals(MainFragment) && !pFragment.equals(MainFragment)){
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragment_container, pFragment).addToBackStack(null).commit();
}
else {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragment_container, pFragment).commit();
}
}
This functionality can be achieved by removing .addToBackStack from your fragment transactions.
Here is the Android reference for back stack functionality within fragments: http://developer.android.com/training/implementing-navigation/temporal.html#back-fragments
I finally get the dessired result just replacing the fragment I want on the back_key onClickListener:
final OnClickListener goBack = new OnClickListener() {
#Override
public void onClick(View v) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container, MainFragment).commit();
}
};
This way, if In the future I need to go back to another fragment instead of the Main, I'll just add a condition to compare fragment tags and do something depending the tag.

Categories

Resources