Best method to load bitmaps in a multi-activity app

I am developing my first android game, it includes a considerable amount of bitmaps. Everything was totally fine until I decided to add a "main activity" to my app that sits behind the game activity.

Before:

[Game Activity] --> SurfaceView --> init (loading bitmaps) --> starting the game

Now:

[Main Activity] --> (user needs to click on "PLAY" button) -->...
[Game Activity] --> SurfaceView --> init (loading bitmaps) --> starting the game

Here is the problem. Every time the user clicks on PLAY button, a new instance of Game Activity is created and it has to go through the "init" and load the bitmaps all over again... EDIT: for example, if during the game user presses the back button.

Clearly, this is a poorly design architecture. Would anyone be able to suggest a more efficient way?

Answers


Use a static class to store your bitmaps, call the load() method when your game firsts starts (maybe during a splash screen), this method also ensures you only ever load each bitmap into memory once.

For example:

public class Art {

    public static Bitmap enemy;
    public static Bitmap player;

    public static void load () {
        enemy = loadBitmap('enemy.png');
        player = loadBitmap('player.png');
    }

    private static Bitmap loadBitmap(String filename) {
        //create and return bitmap here
    }

}

Then when you create a game object you can just call Art.player to get your player image.

And maybe, you shouldn't be using two Activities but just move around layouts when the user hits play.


Need Your Help

Knockout Has-focus with multiple inputs

javascript html knockout.js

I am using knockoutjs and in my Template i have a foreach loop that will display the data it grabs from the JSON data i want to set the focus to the first input on the screen it works perfectly when