Reputation: 6571
First!
Ok, so am working on a app "update system" for an application that is not hosted on the store. What I would like to do is launch the browser from an activity and then exit the application, leaving the browser open. Can this be done, and if so can you point me in the right direction?
Edit:
I dont know if this changes anything put I wish to do so from an AlertDialog.
Upvotes: 4
Views: 1641
Reputation: 28417
This is how you launch a browser:
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.somewebsite.com"));
startActivity(browserIntent);
And this is how you finish your activity:
finish();
Finishing an activity is not the same as quitting your whole application, because there could be many activities in the stack. However, you can (and you should) leave this task to the system - your app's process will be killed automatically when there is not enough resources available. For reference: Quitting an application - is that frowned upon?
Edit: sample 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();
Upvotes: 5
Reputation: 363
You need to create a new intent to open the browser on your device and finish the current activity after you have launched the new intent.
Intent browserIntent = new Intent(Intent.ACTION_VIEW);
browserIntent.setData(Uri.parse("http://www.google.com"));
startActivity(browserIntent);
finish();
Upvotes: 1