How to rerun onCreate, after user selects preference.

859 views Asked by At

So to make things simple my app consists of a bunch of cards (Google style). In the settings the user can select which card is viewable in the main activity. My problem is that I cant figure how to update the main activity after the user selects which cards are enabled.

Hopefully this diagram explains what I'm trying to say.

main activity --> (menu button) --> settings (menu item) --> preference fragment (settings screen) --> (user selects which cards are enabled) --> (back button) --> (main activity should reflect the changes)

I have looked into using the onResume method, and then calling recreate() inside of it. Not sure if I'm heading the right direction. The app crashed after I added that.

Main Activity.java

public void onCreate(Bundle savedInstanceState) {
        //remove title bar
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // init CardView
        mCardView = (CardUI) findViewById(R.id.cardsview);
        mCardView.setSwipeable(true);

        CardStack freqUsedGroup = new CardStack();
        freqUsedGroup.setTitle("Easy Access Cards");
        mCardView.addStack(freqUsedGroup);

        loadPref();
        //Begin UMass Website Card

        MyPlayCard umassEduCard = new MyPlayCard("UMass.edu",
                "Click this card to access UMass Amherst Website", "#881c1c",
                "#CC0000", true, true);
        umassEduCard.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse("http://www.umass.edu/"));
            startActivity(intent);

            }
            });
        //end UMass Website Card

           if(my_checkbox_preference == true)
        {
            mCardView.addCard(umassEduCard);
        }
    mCardView.refresh();
}

public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    Intent intent = new Intent();
    intent.setClass(MainActivity.this, SetPreferenceActivity.class);
    startActivityForResult(intent, 0); 

    return true;
 }


private void loadPref() {
      SharedPreferences mySharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

      my_checkbox_preference = mySharedPreferences.getBoolean("umass_website", false);

     }

SettingsFragment.java

public class SettingsFragment extends PreferenceFragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Load the preferences from an XML resource
        addPreferencesFromResource(R.layout.preferences);
    }
}

Thanks, let me know if any more code is need like xml, etc.

1

There are 1 answers

3
Budius On

You should use the startActivityForResult mechanism.

Here some pseudo-code:

MainActivity -> startActivityForResult(Settings activity)
settingActivity.onCreate() -> setResult(RESULT_CANCELED)
if any settings is actually changed -> setResult(RESULT_OK)
mainActivity.onActivityResult -> if( RESULT_OK )
      // you can try to actually change stuff or, make it a pretty simpler.
      // just re-start the whole thing
      startActivity(new Intent(this, this.class));
      finish();

edit:

from the fragment you can call the setResult method like this

// inside SettingsFragment 
// whenever something changed in the settings and you want to re-start the MainAcitivty
getActivity().setResult(RESULT_OK);
// simple!

still is the MainActivity and the SetPreferenceActivity that are doing the "activity result" communication, by the SettingsFragment is that calls the shots ;)