문제

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