0

I am trying to get my admob id from a php file on my web server. To do this I have setup android volley on my splash activity and it successfully gets the admob id.

However I want to set the response from android volley as the admob id for the rest of my activities. I am unsure how to set the admob is as almost like a string so any other activity can use it.

Please can someone help me as I want it be able to change my admob id remotely

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • Store the string in sharedPreferences and load from it in every new activity.. https://stackoverflow.com/a/12074219/940834 – IAmGroot Feb 11 '19 at 16:38
  • How would I do that? – Mohammed Salaam Feb 11 '19 at 16:39
  • Added link to example. Call save on it on your first screen, passing in your admob id. And then call load, in onCreate and use accordingly. – IAmGroot Feb 11 '19 at 16:39
  • Thankyou. So to save it I set a name for the string and the string itself which for me would be “response”. And to retrieve it I input the name for the string and something such as “error”. Is this correct? – Mohammed Salaam Feb 11 '19 at 16:41
  • Pretty much yes. You could return null instead of "error".. and if you get null back (instead of your saved ID) then do something accordingly? But yes. that would work. Just to clear, you would put your String varaible `response`, not the text "response" – IAmGroot Feb 11 '19 at 16:43
  • When I try it I type context and it comes up in red? How do I fix this? – Mohammed Salaam Feb 11 '19 at 16:55
  • Where I say getDefaultSharedPreferences(context) – Mohammed Salaam Feb 11 '19 at 16:56
  • `getDefaultSharedPreferences(this)` if called from an activity. You need to pass in your `context`. If you are calling it inside an async task, you will need to store your context in a paramter and pass it through. – IAmGroot Feb 11 '19 at 17:04
  • I’m relatively new to android studio and I don’t actually know what you mean by context. And how do I store my context in a parameter. Please could you advise me further on how to do this. – Mohammed Salaam Feb 11 '19 at 17:06
  • This might help https://stackoverflow.com/questions/3572463/what-is-context-on-android. I have to go, good luck. – IAmGroot Feb 11 '19 at 17:08
  • Thanks for the help. I have successful added the shared preferences on my splash screen to get the admob id from my database. However, when the splash screen loads, the next activity comes up as blank. In the main activity the only change i have made is to request the string from SharedPreferences and set it as admob unit id. How can i fix this? – Mohammed Salaam Feb 11 '19 at 18:24

0 Answers0