Go to main screen from any screen in android studio

-3

I have a project in Android studio with several activities, in which I want the moment when I press the back button of the navigation bar to return to the main screen of my project.

Thank you in advance

    
asked by Armando Escobedo 05.07.2018 в 04:12
source

4 answers

0

If you are opening activities from other activities (in addition to the MainActivity ) then what you have to do is finish those activities with finish() right after the call to startActivity .

That way any activity that is no longer visible will be dead (except for MainActivity).

    
answered by 05.07.2018 в 07:43
0

From my absolute novatez, I imagine that you could overwrite the onBackPressed method and then do an intent to the Activity that you want:

@Override
public void onBackPressed() {
  //Aquí tu Intent...
}
    
answered by 05.07.2018 в 17:26
0

Hello, you should try writing the @back method with an attempt to check if the desired activity already exists in the stack, in this way you would avoid creating unnecessary instances, the flags you can use are Intent.FLAG_ACTIVITY_CLEAR_TOP that would clean the stack later to do the intent and Intent.FLAG_ACTIVITY_SINGLE_TOP that checks if there is activity on the stack

@Override
    public void onBackPressed() {    
    Intent intent = new Intent().setClass(this, HomeActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                    startActivity(intent);
    }
    
answered by 05.07.2018 в 18:50
-1

Simply overwrite the onBackPressed method of the activity

@Override
    public void onBackPressed() {
               startActivity(new Intent(this,MainActivity.class));

    }
    
answered by 05.07.2018 в 13:44