Question

I want to do something on back button pressed. I read that there is a method onBackPressed. I used this method but now I can't close my application with the back button. Here my code:

@Override
public void onBackPressed() {
    mViewpager.setVisibility(View.VISIBLE);
    mylaout.setVisibility(View.GONE);
    return;
}

Pas de solution correcte

Autres conseils

After you've done your stuff, just call super:

@Override
public void onBackPressed() {
    mViewpager.setVisibility(View.VISIBLE);
    mylaout.setVisibility(View.GONE);
    super.onBackPressed();
}

Call the method of the super class when you want to close. Set shouldClose (member variable in your activity, that you have declare on your own) to true if the back button should close the app, false otherwise.

@Override
public void onBackPressed() {
    if(shouldClose){
        super.onBackPressed();
    }
    else {
        mViewpager.setVisibility(View.VISIBLE);
        mylaout.setVisibility(View.GONE);
    }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top