2

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!

pez
  • 3,859
  • 12
  • 40
  • 72
  • use backstack to see who is currently being shown few post [0](http://stackoverflow.com/questions/30416815/can-i-put-a-fragment-on-backstack-which-hasnt-been-initialized-yet/30417083#30417083), [1](http://stackoverflow.com/questions/30686186/android-show-back-button-depending-on-backstack-count/30686674#30686674),[2](http://stackoverflow.com/questions/30493716/is-there-a-way-to-listen-to-fragmenttransactions-of-a-fragmentmanager/30493931#30493931).. some are indirect. im guess you are calling it on the wrong fragment – Elltz Jun 06 '15 at 23:23

1 Answers1

0

1) Use onSaveInstanceState method in MainActivity to save current fragment. Restore it in onCreate method.

public void onCreate(Bundle savedInstanceState) {
    ...
    if (savedInstanceState != null) {
        //Restore the fragment's instance
        mContent = getSupportFragmentManager().getFragment(
                    savedInstanceState, "mContent");
        ...
    }
    ...
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

//Save the fragment's instance
getSupportFragmentManager().putFragment(outState, "mContent", mContent);


}

In the fragment, save instance state by override onSaveInstanceState and restore on onActivityCreated:

public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    ...
    if (savedInstanceState != null) {
        //Restore the fragment's state here
    }
}
...
@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

//Save the fragment's state here


}

Check this

2) In your fragment's onResume method get your shared preferences and set it to views:

SharedPreferences preferences =      this.getActivity().getSharedPreferences("pref", Context.MODE_PRIVATE);
String value = preferences .getString("key", "default_value");
Community
  • 1
  • 1
Anton Kovalyov
  • 932
  • 4
  • 13