p_d
p_d

Reputation: 101

Communicating big objects between several fragments/activities. Interfaces/EventBus best practices

so i have a pretty complex data model that holds all the data i parsed from a ~500 lines XML. I also have two activities, both of them have their own viewpager with 3-5 fragments.

The purpose of the app is to provide an user interface to configure said XML file. What i want to be able to do now is:

Most of the threads i read about this topic were pretty old ~2-4years and suggest doing it with interfaces. Doing it this way, wouldn't i have to implement an interface in the activity for every fragment?

Is there a more efficient way? Or are there any other libs than EventBus available that make communicating between fragments/activities/background threads easier?

cheers

Upvotes: 0

Views: 43

Answers (1)

Magesh Pandian
Magesh Pandian

Reputation: 9369

Dias, better you can create AppDataManager Class that hold all the data. this class like a Singleton. then during switching activities and fragment you can set and get data via AppDataManager Class.

Example:

AppDataManager.java

public class AppDataManager {
private static final AppDataManager ourInstance = new AppDataManager();

private String name;
private AppDataManager() {
}

public void setName(String name) {
    this.name = name;
}

public String getName() {
    return name;
}

public static AppDataManager getInstance() {
    return ourInstance;
}}

In your Activity or Fragment set Data like below,

  AppDataManager appDataManager = AppDataManager.getInstance();
            appDataManager.setName("User1");

In your Activity or Fragment get Data like below,

 AppDataManager appDataManager = AppDataManager.getInstance();
            Log.d(TAG," ===>"+appDataManager.getName());

Upvotes: 1

Related Questions