Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to share String among multiple modules?

I have 3 modules: mod1, mod2 and mod3. Module mod1 has dependencies mod2 and mod3.

I want to have one String (in particular it will be UserAgent because all 3 modules HTTP-talk to some server and all 3 should have the same user agent set) to be shared among those 3 modules.

like image 234
Marian Paździoch Avatar asked Feb 05 '16 14:02

Marian Paździoch


1 Answers

One way to do this is to make a public class with methods to write and read information.

    public static String readnote(Context context, String str) {
    if (context == null) throw new RuntimeException ("Context is null");
    SharedPreferences sp = context.getSharedPreferences("somenameyouchoose", 0);
    String mycontent = sp.getString(str);
    return mycontent;
}

public static String writenote(Context context, String str) {
    if (context == null) throw new RuntimeException ("Context is null");
    SharedPreferences sp = context.getSharedPreferences("somenameyouchoose", 0);
    SharedPreferences.Editor editor = sp.edit();
    editor.putString(str);
    editor.commit();
    return str;
}

Then you can writenote (this, "myUserAgent") and readnote (this, "myUserAgent").

If you use intent, another aproach is to use intent.putExtra.

like image 85
statosdotcom Avatar answered Oct 14 '22 02:10

statosdotcom