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

没有正确的解决方案

其他提示

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);
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top