开发者

Opening a web browser from an Android application

开发者 https://www.devze.com 2023-01-24 01:21 出处:网络
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 bla

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 cod开发者_如何学Ce:

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


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.


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.


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);

}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消