Reputation: 474
Override this function, it return previous page, but at same time it will exit the app. Just press the back button once, anyone can help me solve this problem. Use V4 slide fragment.
public void onBackPressed() {
moveTaskToBack(true);
if (mViewPager.getCurrentItem() == 0) {
super.onBackPressed();
} else {
mViewPager.setCurrentItem(mViewPager.getCurrentItem() - 1);
}
Upvotes: 0
Views: 890
Reputation: 3045
moveTaskToBack(true)
is the responsible for closing your app, if you only want your app to close when you are on the first page of your 'ViewPager' try moving it inside the if statement.
Something like this:
public void onBackPressed() {
if (mViewPager.getCurrentItem() == 0) {
moveTaskToBack(true);
} else {
mViewPager.setCurrentItem(mViewPager.getCurrentItem() - 1);
}
}
Upvotes: 1
Reputation: 164
try this at Activity
:
System.exit(1);
or at other classes you can use this:
context.System.exit(1);
Upvotes: 0
Reputation: 7966
You need use addToBackStack
public void showFragment(View v) {
FragmentA f = new FragmentA();
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.container, f, tag);
ft.addToBackStack(tag);
ft.commit();
}
@Override
public void onBackPressed() {
FragmentManager fm = getSupportFragmentManager();
if (fm.getBackStackEntryCount() > 0) {
fm.popBackStack();
} else {
super.onBackPressed();
}
}
Upvotes: 0