rmaik
rmaik

Reputation: 1086

BackButton in the Fragment cannot be disabled

I have a mainActivity with viewPager and three tabs, each tab has its respective fragment. i want when this mainActivity is launched and the backButton is pressed, nothing to happen.

to achieve this, i override onBackPressed() inside the mainActivity the one has viewPager with tabs, but when the mainActivity ia launched one of the tabs, which represents Fragment, by default appears, and when i press the backButton, i go back, which means the Backbuton is not disabled.

that is it, in the mainActivity, i have:

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();
    Log.w(TAG, "@onBackPressed().");
}

and he backbutton is not disabled in the Fragment, each time i press it, the logCat displays the message in the onBackPressed and i go back, and i do not want this, i want while i am in the Fragment "any one of the tabs", the backButton should has no effect.

please let me know how to achieve this?

Upvotes: 0

Views: 28

Answers (2)

ClarkXP
ClarkXP

Reputation: 1233

If you need avoid the back button, comment the super call //super.onBackPressed();

Upvotes: 1

Sam Bains
Sam Bains

Reputation: 548

Try not calling

super.onBackPressed();

Although the log will still print because the method will be called, this will stop Android performing it's logic and going back.

If you want the fragment tabs to do nothing, but the viewpager to handle it, do something like this

@Override
public void onBackPressed() {
    if (getCurrentTab == 0) {
        super.onBackPressed();
    } else {
        //This will stop the fragment tabs from doing anything when back is pressed
    }
}

Upvotes: 2

Related Questions