Raymond
Raymond

Reputation: 1

Opening a web browser from an Android application

I created an app to simply open a web browser and go to a specific website. However, after it's run for the first time, if I go to another app and run the app again, I will get nothing except the black screen. I knew I can find the page from web browser, however I wonder if there is a way to bring the content back when I launch the app and the user could get back to work from where he/she stopped.

here is my code:

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    /** setup the url and start a browser with 
     *  the hotmial.com and return to the user
     * */
    String url = "http://www.hotmail.com";
    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setData(Uri.parse(url));
    startActivity(i);

}

Thanks guys

Upvotes: 0

Views: 1949

Answers (4)

Bear
Bear

Reputation: 1571

Why don't you use the android.webkit.WebView which will allow you to directly browse web pages in your app. Here the web content will remain persistent as long as the application is running. On the chance that your application stops you could save the url using WebView.getURL() which would allow you to continue from where you left of the next time you start your app. If this is what you want to do let me know and I will post some sample code.

Upvotes: 0

James
James

Reputation: 5642

Try this if you want to open the browser OUTSIDE your app. I know it seems like a simple(and trivial) edit, but I've never had issues setting it up this way.

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    /** setup the url and start a browser with 
     *  the hotmial.com and return to the user
     * */
    String url = "http://www.hotmail.com";
    Uri uri = Uri.parse(url);
    Intent i= new Intent(Intent.ACTION_VIEW, uri);  
    startActivity(i);

}

Upvotes: 0

DavGin
DavGin

Reputation: 8355

The activity may still exist when you run your app a second time; perhaps onCreate is not called.

After you throw the Intent to run the browser, shut down the activity so that onCreate happens every time.

If your application is here only to give a link to a website, this is much overhead in my opinion. Android users can make a shortcut to any website they want on the desktop, the shortcut icon being shown with the site's favicon in its corner.

Upvotes: 2

Bryan Denny
Bryan Denny

Reputation: 27596

Take a look at the activity lifecycle. You may want to move your intent to OnStart() for when the application comes back to the foreground after being stopped.

Upvotes: 3

Related Questions