Search code examples
androidwebviewlaunching-application

Need help creating a simple website launcher for android


I am a beginner with app development using Java and have a requirement which I am not sure how to complete. I need to create a simple launcher that will just launch a specified website in the default browser (essentially the same thing as creating a shortcut from within chrome browser).

I had attempted to create an app already and it launches the browser but it also opens my app which leaves mine running in the background. I have the following in the oncreate:

finish();
moveTaskToBack(true);
System.exit(0);

The other thing I had tried was using webview but the site I am linking to requires authentication and after the first authentication, javascript does not seem to be working, works fine on initial page using browser.getSettings().setJavaScriptEnabled(true)

Thank you in advance,

Alex

UPDATE

protected void onCreate(Bundle savedInstanceState) { 
    Intent viewIntent = new Intent("android.intent.action.VIEW", Uri.parse("yourwebsite.com" )); 
    startActivity(viewIntent);  

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

    finish(); 
    moveTaskToBack(true); 
    System.exit(0); 
} 

Solution

  • Try this out

    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
    
        Intent viewIntent = new Intent(Intent.ACTION_VIEW, 
                Uri.parse("http://www.yourwebsite.com" )); 
    
        startActivity(viewIntent);  
        finish(); 
    } 
    

    You need to call the super method first and foremost in your onCreate method. It will perform the grunt work to get your activity up and running. You don't need to set your content view next, but in this case it will be the next step.

    Next you will build, and start your intent to redirect to the default browser. Good to go there.

    Lastly, you need to call finish. Calling

    finish(); 
    moveTaskToBack(true); 
    System.exit(0);
    

    is like saying kill this activity, bury it in the back, finally exit the scene all together. Though that makes for a great for a horror flick, it is unnecessary (and potentially harmful). Simply calling finish() will notify android that the application should be destroyed and gc'ed.

    UPDATE

    To make sure your app is actually getting killed you could do something like this:

    @Override
    public void onStop(){
        Log.d("Kill Check","STOPPING APP");
        super.onStop();
    }
    
    @Override
    public void onDestroy(){
        Log.d("Kill Check","DESTROYING APP");
        super.onDestroy();
    }
    

    And make sure that the app is being stopped and/or destroyed. It should be, since finish gets called once you start the browser activity.