Kasra
Kasra

Reputation: 3145

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?

Upvotes: 0

Views: 163

Answers (1)

Strigger
Strigger

Reputation: 1903

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.

Upvotes: 1

Related Questions