用户选择首选项后如何重新运行onCreate。

时间:2013-08-25 12:12:58

标签: java android sharedpreferences onresume checkboxpreference

所以为了简单起见我的应用程序包含一堆卡片(Google风格)。在设置中,用户可以选择主活动中可查看的卡。我的问题是,在用户选择启用哪些卡后,我无法确定如何更新主要活动。

希望这张图解释了我想说的内容。

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)

我已经研究过使用onResume方法,然后在其中调用recreate()。不确定我是否正朝着正确的方向前进。在我添加之后,应用程序崩溃了。

主要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);
    }
}

谢谢,让我知道是否需要更多代码,如xml等。

1 个答案:

答案 0 :(得分:1)

您应该使用startActivityForResult机制。

这里有一些伪代码:

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();

修改

来自片段,您可以像这样调用setResult方法

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

仍然是正在进行“活动结果”通信的MainActivity和SetPreferenceActivity,通过SettingsFragment来调用镜头;)