hello, what should be added to the onBackPressed() method in MainActivity so that it would be impossible to go back from the fragment when clicking on BackButton ?

in MainActivity

 @Override public void onBackPressed() { super.onBackPressed(); } 

transition to fragment

 protected Fragment fragment = null; /** * Call to LoadingGame fragment */ public void LoadingGamePath() { fragment = new LoadingFragment(); getSupportFragmentManager().beginTransaction().replace(R.id.container_body, fragment).commit(); } 
  • I have a fragment of the game I need so that the user could not get out of this window when pressing the button back on the phone - David Kern

2 answers 2

In MainActivity override the onBackPressed() method:

 @Override public void onBackPressed() { Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.container_body); if (!(fragment instanceof LoadingFragment)) { super.onBackPressed(); } } 

Then the back button will not respond to a click, only if it is pressed at the moment when the LoadingFragment fragment is in the container.

  • ok, but then it doesn't matter from which class the backbutton doesn't work - David Kern
  • one
    @DavidKern, Updated the answer. - post_zeew Nov.
  • ok works thanks very much I will fix this topic :))) - David Kern

Override the onBackPressed method. Take away the super.onBackPresed method call

  • and where a method to redefine? - David Kern
  • one
    In the fragment redefine it - pavel163
  • one
    @ pavel163, Does the fragment have such a method? - post_zeew
  • no% $ ^% $ ^% $ ^% ^% $ ^ - David Kern
  • I threw in the fragment it removes @Override and nothing happens - David Kern