gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 03:24:44 25 4
gpt4 key购买 nike

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

希望这张图能解释我想表达的意思。

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 等,请告诉我。

最佳答案

您应该使用 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();

编辑:

您可以从 fragment 中调用 setResult 方法,如下所示

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

仍然是MainActivity和SetPreferenceActivity在做“Activity 结果”的沟通,由SettingsFragment做主;)

关于java - 如何在用户选择首选项后重新运行 onCreate。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18428843/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com