I\'m trying to override the Back Button because it\'s closing my app when I push on, I have different Fragments:
onBackPressed()
and getSupportFragmentManager()
are methods from the Activity class, not Fragment, so you have to implement this on the Activity and not the Fragment.
If you want to implement a particular behaviour just for one fragment you can check what's the current visible fragment and then implement your behaviour. Like this:
@Override
public void onBackPressed(){
MyFragment myFragment = (MyFragment)getFragmentManager().findFragmentByTag("YOUR_FRAGMENT");
if (myFragment != null && myFragment.isVisible()) {
// the code for whatever behaviour you want goes here
}
else
super.onBackPressed();
}
Another possible, probably simpler way would be to use some flag in the Activity that you activate when you add the fragment and then read from the onBackPressed()
.
EDIT ON RESPONSE TO BENJY'S EDIT
Notice that you're not exactly using the code I posted. You're using findFragmentById()
which doesn't apply to every situation and I can't tell if it's right for your code.
You should use findFragmentByTag
which applies to any kind of fragment transaction.
Just add a tag to your fragment when you do the transaction, like this:
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment(), "FRAGMENT_TAG")
.commit();
Then when you try to get back the Fragment in your onBackPressed()
, you get it looking for that tag:
PlaceholderFragment myFragment = (PlaceholderFragment) getSupportFragmentManager()
.findFragmentByTag("FRAGMENT_TAG");
Make this change it has to work.
This is what I use when navigating between fragments:
MainActivity.java:
@Override
public void onBackPressed() {
// note: you can also use 'getSupportFragmentManager()'
FragmentManager mgr = getFragmentManager();
if (mgr.getBackStackEntryCount() == 0) {
// No backstack to pop, so calling super
super.onBackPressed();
} else {
mgr.popBackStack();
}
}
EDIT the second: Please note that you ONLY want to call super.onBackPressed()
if you haven't already handled it (by, for example, popping the fragment manager's backstack).
For that to work, you have to add new fragments to your FragmentManager
's backstack (addToBackStack()
). For example (also in MainActivity.java):
private void displayView(int position) {
Fragment fragment = ...; // YOUR CODE HERE
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, fragment);
// ADD THIS LINE
fragmentTransaction.addToBackStack("name"); // name can be null
fragmentTransaction.commit();
// libelle du toolbar
TextView titlet;
titlet = (TextView) findViewById(R.id.main_toolbar_title);
titlet.setText(title);
titlet.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/GothamBook.ttf"));
}
}
EDIT the third (7/28): In your onCreate(Bundle)
method, you do your very first fragment transaction by calling your displayView(int)
method. displayView(int)
always adds its fragment transactions to the backstack. This is not what you want. The very first fragment transaction should use fragmentTransaction.**add**(int, Fragment)
and should not call addToBackStack(String)
. Every transaction after the first should call fragmentTransaction.**replace**(int, Fragment)
and should call addToBackStack(String)
. The reason for this is that you first transaction is basically "adding" a fragment (your UI) to an empty container (it is not "replacing" another fragment). When this transaction is on the backstack, that means that the empty-container state is also on the backstack. So when you pop that last transaction, it displays a blank UI.
EDIT the first: When you call addToBackStack(String name) on a FragmentTransaction object (which you obtain by calling getFragmentManager().beginTransaction()), then you are adding a FragmentTransaction
to your FragmentManager
s 'backstack'. What my code does is check the size of the backstack by calling getFragmentManager.getBackStackEntryCount(). If that number is greater than zero, then we know we have FragmentTransaction
s on the backstack. In such a case, you can call getFragmentManager.popBackStack(), which will pop the last transaction off the backstack--in other words, returning your app to the last Fragment that was on display.
If the backstack entry county equals 0, then that means you're at your Fragment A, and you should instead call super.onBackPressed()
, and this will cause the app to exit.
I know this too late for answer, but whoever, visits here, might got help with this, I achieved this through below.
if you want to change the fragment keeping the activity on click on device back button, above mentioned all codes will work with removing below line.
super.onBackPressed()