4

I am trying to pass information (more specifically a class with information) from one activity to another. In my application I have a splash screen that is responsible for loading and initializing variables. The goal is to get this information to the actual game itself so it may be used but I don't know how to do so. In my splash screen class I have this method that is responsible for moving from the splash screen to the game once everything is loaded:

private void moveToGame() {
    loop.setRunning(false);
    act.startActivity(new Intent(splash, MainActivity.class));
    act.finish();
    return;
}

The main activity class then has this line of code to get to the actual game:

setContentView(new Environment(this, this));

The constructor for the Environment class is Environment(Context context, Activity act)

The goal is to change the constuctor to Environment(Context context, ActivityAct, LoadInfo li) but how do I pass the information all the way to the Environment constructor?

EDIT 1 - LoadInfo Class

public class LoadInfo {
    private HashMap<String, Typeface> fonts;
    private HashMap<String, Image> images;
    private File logFile;
    private File settingsFile;
    private File gameDir;

    public LoadInfo() {}

    public LoadInfo(HashMap<String, Typeface> fonts, HashMap<String, Image> images, File logFile, File settingsFile, File gameDir) {
        this.fonts = fonts;
        this.images = images;
        this.logFile = logFile;
        this.settingsFile = settingsFile;
        this.gameDir = gameDir;
    }

    public HashMap<String, Typeface> getFonts() {
        return fonts;
    }

    public HashMap<String, Image> getImages() {
        return images;
    }

    public File getLogFile() {
        return logFile;
    }

    public File getSettingsFile() {
        return settingsFile;
    }

    public File getGameDir() {
        return gameDir;
    }

    public void setFonts(HashMap<String, Typeface> fonts) {
        this.fonts = fonts;
    }

    public void setImages(HashMap<String, Image> images) {
        this.images = images;
    }

    public void setLogFile(File logFile) {
        this.logFile = logFile;
    }

    public void setGameDir(File gameDir) {
        this.gameDir = gameDir;
    }

    public void setSettingsFile(File settingsFile) {
        this.settingsFile = settingsFile;
    }

    public boolean fullyLoaded() {
        return fonts != null && images != null && logFile != null && gameDir != null && settingsFile != null;
    }

    public String toString() {
        if(logFile == null)
            return "well no file to load";
        return logFile.toString();
    }
}
Ryan
  • 727
  • 2
  • 14
  • 31

2 Answers2

3

You can make your LoadInfo as Serializable like below,

public class LoadInfo implements Serializable {
    // your code,
}

and in you Splash Activity you can send like this,

//LoadInfo loadInfo = new LoadInfo(); this may be your loadInfo object

Intent intent = new Intent(act, MainActivity.class);
intent.putExtra("load_info", loadInfo); // Add your LoadInfo object here
act.startActivity(intent);

In your MainActvity you can get like this,

LoadInfo loadInfo = (LoadInfo) getIntent().getSerializableExtra("load_info");
setContentView(new Environment(this, this, loadInfo));
Muthukrishnan Rajendran
  • 11,122
  • 3
  • 31
  • 41
  • This would seem to work, but I am getting an error that I don't know whether or not it is a result of this. I get `NullPointerException` when I try to call `loadInfo.toString()` including in the splash screen class where the original `LoadInfo` class was created. In fact I get `NullPointerException`s for every "get" method even though I checked to make sure that the object (in this case a `File`) was not null. Is this a result of this or some other problem that is now evident? – Ryan Aug 06 '17 at 00:18
  • Add your code in your question, I will check and let you know – Muthukrishnan Rajendran Aug 06 '17 at 00:19
  • Done, if you need more context let me know. Thank you for all your help – Ryan Aug 06 '17 at 00:26
  • Are you adding setGameDir before calling moveToGame() in SplashScreen..? – Muthukrishnan Rajendran Aug 06 '17 at 00:29
  • Turns out the answer is yes and no. I was but there was a chunk of code elsewhere that I meant to delete that reset the `loadInfo` object causing the problems. Removed it and everything is working perfectly now. Thanks so much for your help – Ryan Aug 06 '17 at 00:35
  • You are Welcome, Friend :) – Muthukrishnan Rajendran Aug 06 '17 at 00:37
0

Warning: Intent extra has a limit of 1Mb:

To pass information from one Activity to another it's normal to use Intent extra, but it has a limitation of 1MB of data. In your question, you are using LoadInfo class and I believe it can easily pass 1MB in size because it loads Game information.

Suggestion: You can choose to implement Application or Service (i.e. Bound Service) to store this instance of LoadInfo and all your activities can access this instance.

More Tip: You can also use a Singleton class that stores this instance ofLoaderInfo, but you should remove it after closing all activities of your game.

Obs: Splash Screen as an Activity, you must remember to remove it from Back Stack, otherwise, when the user clicks back, it will return to the splash Activity.

Victor Rattis
  • 534
  • 5
  • 10
  • I will look into those suggestions because I agree that the `LoadInfo` class could easily pass 1MB as well. And as for the Splash Screen as an `Activity`, I haven't encountered that problem in emulators or physical devices. – Ryan Aug 06 '17 at 03:20
  • You are right because you are using `act.finish();`, that will destroy this `Activity` and so removing it in `Back Stack`. – Victor Rattis Aug 06 '17 at 11:12