s.k.paul s.k.paul - 1 year ago 70
Android Question

Appropriate method to read shared preference and show/hide controls in android

I would like to execute the following codes-

sharedPrefs = getSharedPreferences("MyConfig", MODE_PRIVATE);
BrId = sharedPrefs.getInt("BrId", 0);

if (BrId == 0){
//Want to show/hide some controls
}


What is the best practices to place these? in
onCreate()
or
onStart()
?

Answer Source

Condition 1:

If this logic needs to populate some UI components based on the result and just once before UI rendering , then the answer is onCreate()

Condition 2:

Whenever the Activity is in foreground and you need some logic , then onResume() would prefer. Some other Activity may get pushed on top of this Activity and the value changes due to some business logic. Now its should be in onResume()

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download