1

In my application i have 4 fragments like the follow [A] [A-sub] [B] [B-sub] when the user press back button from [A] or [B] it should exit the app suppose the user press back button from [A-sub] or [B-sub] it should redirect the app to [A] or [B]

I hope you got my problem

i have tried the follow in my main fragment activity but i know its only the solution for exit the app

private boolean _doubleBackToExitPressedOnce    = false;

@Override
public void onBackPressed() {

System.out.println("onBackPressed--");
if (_doubleBackToExitPressedOnce) {
    super.onBackPressed();
    return;
}
this._doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Press again to quit", Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {

        _doubleBackToExitPressedOnce = false;
    }
}, 2000);
}
Aristo Michael
  • 2,166
  • 3
  • 35
  • 43

1 Answers1

1

Do something like this, override onBackpessed()

@Override
    public void onBackPressed()
    {
        if (page == SUB_FRG) 
            super.onBackPressed();//Normal Back
        else 
        //Finish activity holding fragment
    }

when u are replacing fragment to subfragment use addToBackStack(null) like following

FragmentManager myProfilefragmentManager =  getSupportFragmentManager();
    FragmentTransaction myProfilefragmentTransaction = myProfilefragmentManager.beginTransaction();
    myProfilefragmentTransaction.replace(R.id.article_fragment, myProfileFragment).addToBackStack(null).commit();
keshav
  • 3,235
  • 1
  • 16
  • 22