AlexF11
AlexF11

Reputation: 243

Recreate a created Fragment?

I have an activity that writes some data to sharedpreferences after recieving it from a web service and also displays fragments. Those fragments use a method that needs those sharedpreferences to then populate the fragments layout.

The problem is that when the fragment initially gets created those shared preferences haven't been written yet and without them the fragment is blank. How can I recreate the fragment after I write to sharedpreferences?

Upvotes: 2

Views: 3280

Answers (1)

Larry Schiefer
Larry Schiefer

Reputation: 15775

No need to re-create the Fragment, that would be a very heavy operation. Use SharedPreferences.registerOnSharedPreferenceChangeListener() from your Fragment to register a callback to be notified when changes occur. Then in your Fragment, provide an instance of something which implements SharedPreferences.OnSharedPreferenceChangeListener. See registerOnsharedPreferenceChangeListener and OnSharedPreferenceChangeListener.

Upvotes: 1

Related Questions