1

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

希望这张图能解释我想说的。

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() 。不确定我是否朝着正确的方向前进。我添加后应用程序崩溃了。

主要活动.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 等。

4

1 回答 1

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 做主;)

于 2013-08-25T13:14:17.183 回答