Search code examples
androidlauncher

Android - a better website launcher native app


I have developed a very simple Android native app to launch my HTML5 webapp. Here is the main class:

package com.plancake.android.app;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

public class PlancakeActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // setContentView(R.layout.main);

        Intent viewIntent = new Intent("android.intent.action.VIEW", Uri.parse("https://www.mywebsite.com"));  
        startActivity(viewIntent);   
    }
}

When I tap its icon from my homescreen the first time everything works fine: Android opens up the browser and my webapp is loaded. Lovely!

The problem arises when I leave the webbrowser and I tap again on my launcher icon from the home screen or task manager. In that case, I get just a black screen.

I think the problem is that Android calls the onResume method (or something else), anyway not the onCreate in this latter case.

I can see two solutions:
1) Either I replicate the startActivity call in all other methods, such as onStart, onRestore - but in that case I am afraid my app will create a new browser window everytime, therefore consuming a lot of memory unnecessarily
2) Or after each tap, force my launcher app to leave the memory so that the onCreate method is called everytime I tap - but in this case, even if I "kill" my app, the browser will be still open so next time I tap on my launcher icon a new browser window will be created anyway

I hope I was clear enough in describing my problem and the solutions I thought of - which either of them are ideal.

Please, I would like to know how you would suggest to solve my problem.

Thanks,
Dan


Solution

  • The onResume() method is always called after onCreate().

    If you leave your activity and it is not killed by the system, then it might not be "created" again. It is left in a paused state.

    What you should do is move your code to onResume() to ensure that the intent creating the webview is called every single time you enter your application