Close all the Activities

Asked

Viewed 1,950 times

4

I own 3 activities:

  • splash_screen
  • MainActivity
  • error_webview

To Activity error_webview has two buttons, one to open the splash_screen and another to close (Exit), but wanted to click on "Exit" to close all activities and the app wasn’t open.

Below the source code of mine error_webview for anyone who can help me.

package conectaluziania.conectaluziania;

import android.app.Activity;
import android.content.ComponentCallbacks;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import prosportacademia.prosportacademia.MainActivity;
import prosportacademia.prosportacademia.R;
import prosportacademia.prosportacademia.splash_screen;

public class error_webview extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_error_webview);

/*Abre uma nova atividade Caso a internet não funcione*/
        OnClickListener listnr=new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i= new Intent("splash_screen");
                startActivity(i);
            }
        };
        Button btn =(Button) findViewById(R.id.button);
        btn.setOnClickListener(listnr);

/*Fim da Abertura da Atividade */

/*Inicio: Fecha a atividade quando clicar em Sair */

/*Fim: Fecha a atividade quando clicar em Sair */




    }
}

2 answers

1

This response was made on the basis of Soen, but I added some details.

First, let’s go back to the Activity initial (the one that starts next to your App):

Intent intent = new Intent(getApplicationContext(), splash_screen.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);

Case your splash_screen be on your backstack, to flag FLAG_ACTIVITY_CLEAR_TOP will shut down all the others Activity and return to the initial. If you have closed the splash_screen to start the MainActivity, just change the class in the code.

The putExtra is sending a message to your splash_screen (or MainActivity) which must have the following code in onCreate:

if (getIntent().getBooleanExtra("EXIT", false)) {
    finish();
}

The getBooleanExtra is simply looking for a defined value for the EXIT. If it exists (what will happen when you click the exit button), the Activity will be closed.

That is: first we close all Activity sending a signal to the first of the backstack close, then she will receive the signal and also end, with that there will be no Activity open and the application will be closed.

0

Have an example here [SOLUTION 2] that explains exactly what you want to know!

Another way, if you close Activity and the app, could be:

finish();
System.exit(0);

on the button Switch.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.