I have an activity, MainActivity
, that swaps between seven fragments. The fragments have no particular order, so when the app is launched and the activity is first created, I set one as the default/start screen:
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.fragmentDrawerContainer);
if (fragment == null)
{
fm.beginTransaction()
.add(R.id.fragmentDrawerContainer, new DefaultFragment())
.commit();
// An int I use to track which Fragment is currently being viewed,
// for navigation drawer purposes
mCurrentPosition = DEFAULT_FRAGMENT_POSITION;
}
From the navigation drawer, the user can also go to a new activity, SettingsActivity
which hosts a PreferenceFragment
to change certain settings, such as measurement units (metric vs. imperial) and color themes.
// Standard navigation from one activity to another from inside selectItem() method of nav drawer
Intent intent = new Intent(this, SettingsActivity.class);
startActivity(intent);
Once the user navigates back to MainActivity
from SettingsActivity
, either by back or up button, I need two things to happen:
1) The fragment the user was looking at last must still be there. Currently, the activity reloads DefaultFragment
(because the activity is being recreated, I think).
2) Each fragment contains a custom View I've written, and the View must update itself using values from SharedPreferences
after the user returns from SettingsActivity
.
To solve #1, I've tried using android:launchMode="singleTop"
, which works, but I can't get the Views to refresh unless I switch to another fragment and then come back.
I've tried calling myView.invalidate()
in the onResume()
method of the fragments, but it doesn't seem to work.
Any ideas? Please let me know if I was unclear. Thank you in advance!