Controlling Android Boot Sequence

On most platforms a DukeScript application is a regular Java application - e.g. it has a main method that handles the initialization and then starts the appropriate presenter. If you use our archetypes, you’ll notice the Main or iOSMain classes that do exactly this.

However one platform is different - there is no main method on Android, you need to create own activity. By default DukeScript provides you an activity which handles everything behind the scene automatically. This works fine, until you realize you need some special service from the Android runtime. Such service is usually only available via an instance of an activity. And you don’t have such instance!

Update: Since February 2017, you can get that instance easily - read about enhancements in 0.17 archetypes and see how they can simplify the boot of a DukeScript Android application!

Recently there has been a discussion on our forum and here is the best solution to the problem: create a decorating activity:

public class AndroidMain extends Activity {
    public AndroidMain() {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // obtain information you need
        //
        SharedPreferences prefs = getApplicationContext().getSharedPreferences("karel.prefs", 0);
        new AndroidStorage(prefs);

        try {
            // delegate to original activity
            startActivity(new Intent(getApplicationContext(), Class.forName("com.dukescript.presenters.Android")));
        } catch (ClassNotFoundException ex) {
            throw new IllegalStateException(ex);
        }
        finish();
     }
   }
}

You’ll also need to register this Activity in your AndroidManifest.xml. The full diff of required changes is available in one of Jaroslav’s (the designer of HTML/Java API) pet projects. Check it out here: Karel the Game.