Java – Launch the browser from the activity and exit the application to keep the browser open

Launch the browser from the activity and exit the application to keep the browser open… here is a solution to the problem.

Launch the browser from the activity and exit the application to keep the browser open

First of all!
Ok, I’m developing an app “update system” for an app that isn’t hosted in the store. What I want to do is launch the browser from an activity and then exit the application to leave the browser open. Can this be done? If you can, can you point in the right direction?

Edit:
I don’t know if this will change anything I would like to do with AlertDialog.

Solution

This is how the browser is launched:

Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.somewebsite.com"));
startActivity(browserIntent);

This is how you complete the activity:

finish();

Completing an activity is not the same as exiting the entire application because there can be many activities in the stack. However, you can (and should) leave this task to the system – your app process will automatically terminate when there aren’t enough resources available.
For citation: Quitting an application – is that frowned upon?

EDIT: Example with AlertDialog:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Launch a website?");
builder.setPositiveButton(getString(R.string.yes),
    new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            Intent browserIntent = new Intent(Intent.ACTION_VIEW,
            Uri.parse("http://www.somewebsite.com"));
            startActivity(browserIntent);
            finish();
        }
    }
);
builder.setNegativeButton(getString(R.string.no),
    new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            some other thing to do
        }
    }
);

AlertDialog dialog = builder.create();
dialog.show();

Related Problems and Solutions