1

I'm generating a random string in MainActivity. When i'm using intent method, I can calling and put this string in SecondActivity. But i can't call this string from all activities. How can i do this

protected String getSaltString() {
        String SALTCHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
        StringBuilder salt = new StringBuilder();
        Random rnd = new Random();
        while (salt.length() < 18) { // length of the random string.
            int index = (int) (rnd.nextFloat() * SALTCHARS.length());
            salt.append(SALTCHARS.charAt(index));
        }
        String saltStr = salt.toString();
        return saltStr;

    }
Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53
Walrus
  • 25
  • 7
  • You must use public constant field in your activity or you can add static to the method declaration and make it public – Amir Hossein Jul 07 '19 at 08:56
  • Use EventBus library instead of intents. You can learn how to use it. It's easier.than intents https://www.youtube.com/watch?v=WnzSkRinnuc – JDevoloper Jul 07 '19 at 08:57

2 Answers2

2

Several options are available to you - in this case, because it wont introduce any extra dependencies (RxJava, EventBus, etc.), I'd recommend you write it to SharedPreferences, and have your other activities register a listener on SharedPreferences for changes to your value:

public class SaltStringPreference {

    private static final String KEY = "a_key";

    private final SharedPreferences prefs;
    private Listener listener;

    private SaltStringPreference(SharedPreferences prefs) {
        this.prefs = prefs;
    }

    public static SaltStringPreference from(Context context) {
        return new SaltStringPreference(PreferenceManager.getDefaultSharedPreferences(context));
    }

    public void set(String saltString) {
        prefs.edit().putString(KEY, saltString).apply();
    }

    public String get() {
        return prefs.getString(KEY, "<no_value>");
    }

    /* 
    Note we need to keep a reference to the listener somewhere,
    otherwise it could be garbage collected, see
    https://stackoverflow.com/a/3104265/1219389
    */  
    public void setListener(Listener listener) {
        this.listener = listener;
        prefs.registerOnSharedPreferenceChangeListener(listener);
    }

    public void removeListener() {
        prefs.unregisterOnSharedPreferenceChangeListener(listener);
        this.listener = null;
    }  

    public static abstract class Listener implements SharedPreferences.OnSharedPreferenceChangeListener {
        @Override
        public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
            if(KEY.equals(key)) {
                onSaltStringChanged(sharedPreferences.getString(KEY, "<no_value>"));
            }
        }

        /**
        * Called when the 'salt string' value changes in SharedPreferences
        * @param saltString The new 'salt string' value
        */
        protected abstract void onSaltStringChanged(String saltString);
    }   
}
class ActivityOne extends AppCompatActivity {
    //...
    SaltStringPreference.from(this).set(generatedSaltString);
}
class AnotherActivity extends AppCompatActivity {
    private final SaltStringPreference.Listener listener = new SaltStringPreference.Listener() {
        @Override
        protected void onSaltStringChanged(String saltString) {
            //Do something with new String...
        }
    }); 
    private SaltStringPreference pref;

    //onCreate...
    pref = SaltStringPreference.create(this);
    pref.setListener(listener);

    //onDestroy...
    pref.removeListener();
}
PPartisan
  • 8,173
  • 4
  • 29
  • 48
0

Use static Method, You can call the method to all activities.

public Class UtilClass{
  public static String getSaltString() {
     String SALTCHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
     StringBuilder salt = new StringBuilder();
     Random rnd = new Random();
     while (salt.length() < 18) { // length of the random string.
        int index = (int) (rnd.nextFloat() * SALTCHARS.length());
        salt.append(SALTCHARS.charAt(index));
     }
     String saltStr = salt.toString();
     return saltStr;
   }
}
Sana
  • 360
  • 3
  • 13
  • The problem here is that you'll be able to generate random strings from anywhere in the code, but every time you generate one string it'll be different from all the others. Maybe a statically initialized string would be better: `public static String randomString = getSaltString();` – Some random IT boy Jul 07 '19 at 10:41