I am using a navigation drawer and fragments to inflate the main activity.
However when I press the back button it closes the application which is obvious with fragment setup.
What I want is that when the user clicks the back button they go to the home page fragment if its not currently the view, but if they are on the home page I want the app to close.
How to go around this problem?
Overriding the back button would be a REALLY BAD idea, as it will hurt more than help.
If you want to add a Fragment that will not be replaced (Your initial "HomePageFragment"), just add this line:
Which will give you something like:
final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.fragment, fragment, TAG_FRAGMENT); transaction.addToBackStack(null); transaction.commit();
If you add some other Fragments, don't add them to the back stack.