Android: transferring information from one activity to another

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 in the actual game itself so that it can be used, but I don't know how to do it. In my splash screen class, I have this method, which is responsible for transitioning from splash screen to game after loading:

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 navigate to the actual game:

setContentView(new Environment(this, this));

      

Class constructor Environment

mattersEnvironment(Context context, Activity act)

The goal is to change the constuctor to Environment(Context context, ActivityAct, LoadInfo li)

, but how do you pass information up to the constructor Environment

?

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();
    }
}

      

+3


source to share


3 answers


You can make your LoadInfo as Serializable like below,

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

      

and you can activate Splash Activity 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 the following:

LoadInfo loadInfo = (LoadInfo) getIntent().getSerializableExtra("load_info");
setContentView(new Environment(this, this, loadInfo));

      

+3


source


Warning: Intent

extra has a 1Mb limit :

To pass information from one Activity

to the other, it is common to use Intent

extra, but has a limit of 1 MB of data. In your question, you are using a class LoadInfo

and I believe it can easily traverse 1MB because it loads game information.

Offer . You can implement Application

or Service

(i.e. Bound Service

) to keep this instance LoadInfo

and all your actions can access this instance.



More hints: You can also use a class Singleton

that stores this instance LoaderInfo

, but you must delete it after closing all your game's activities.

Indicate: Screensaver as a quality Activity

, you have to remember to remove it from Back Stack

, otherwise when the user clicks on it, it will revert to splash Activity

.

0


source


In an exercise directed at a tour sender, you should do this little example:   String username="Christian"; int code=123456; Intent i=new Intent(this,ReceiverActivity.class); i.putextra("user",username); i.putextra("code",code);

And in ReceiverActivity, you will get information like this:   Intent i=getIntent(); String usr=i.getStringExtra("user"); int code=i.getIntExtra("code");

-2


source







All Articles