Here's my onResume
call in the MainActivity
:
@Override
public void onResume() {
super.onResume();
SharedPreferences settings = context.getSharedPreferences(INSTALL_PREFERENCE, 0);
String referrerString = settings.getString(REFERRAL_KEY, null);
Map<String, String> params = new HashMap<~>();
if (referrerString != null){
params.put("referrer", referrerString);
}
}
Here's my onResume
in the class that extends BroadcastReceiver
:
@Override
public void onReceive(Context context, Intent intent){
try{
String referrerString = intent.getStringExtra("referrer");
if(null != referrerString){
String referrer = URLEncoder.encode(referrerString, "UTF-8");
context.getSharedPreferences(INSTALL_PREFERENCE, Context.MODE_PRIVATE).edit().putString(REFERRAL_KEY, referrer).commit();
}
}
catch (Exception e){
//don't handle exceptions for now
}
}
The issue is that when I call the MainActivity
by opening the app, my app will not return the referrer
on the first open. Is there a reason the SharedPreferences
will not store my referrer
on the first application open? I want to pass back the referrer
on the first open and not the second.
on resume() will be executed before the broadcast receiver and hence for the first time you might be receiving null or default value for the referrer and at later pont of time if you reopen the activity you might be getting correct value because onReceive() would have executed by this time.
If your application has any settings preference screen which actually initialises the settings only after opening it so you may have to initialises all the preferences with default values before using it. It could be the reason that sometimes if you open the application you are getting correct values because you might have opened the setting screen.