我已经将Deitel的“Flag Quiz”(针对程序员的Android ... App Driven ...)扩展到(除其他外)允许用户显示一个小标志以与给定标志进行比较。用户点按SHOW FLAG
,然后获取国家/地区列表,点击一个,然后返回并显示(如下所示)。
我遇到的唯一问题是,点击所需的国家/地区后,会显示Settings
首选项屏幕(见下文)。我想这是因为SHOW FLAG
是其中的一部分,但我希望用户从列表中选择国家后看到的下一件事就是上面的屏幕。 无需查看下面的屏幕,因此肯定没有理由不必按后退按钮 - 如何避免? < / p>
仅供参考,这里有一个跟踪和相关的代码片段(我认为没有理由显示任何xml
):
MainActivity.java
:
07-06 17:25:48.305 32269-32269/com.dslomer64.flagquiz W/opOptionsItemSelected? item is <Show flag> -----------------------
07-06 17:25:48.350 32269-32269/com.dslomer64.flagquiz W/Here's where? to get flag
07-06 17:25:48.688 32269-32269/com.dslomer64.flagquiz W/onOptionsItemSelected? Show flag showFlag is true
`FlagActivity.java`:
07-06 17:25:48.768 32269-32269/com.dslomer64.flagquiz W/FlagAct? onCreate
07-06 17:25:51.707 32269-32269/com.dslomer64.flagquiz W/returnToA from FlagAct? Anguilla!!!
`SettingsActivity`:
07-06 17:25:51.751 32269-32269/com.dslomer64.flagquiz W/onCreate? SettingsActivity
(not me)
07-06 17:25:51.783 32269-32269/com.dslomer64.flagquiz W/Resources? Converting to string: TypedValue{t=0x10/d=0x3 a=-1}
07-06 17:25:51.786 32269-32269/com.dslomer64.flagquiz W/Resources? Converting to string: TypedValue{t=0x10/d=0x7d0 a=-1}
`MainActivity.java`:
07-06 17:25:55.554 32269-32269/com.dslomer64.flagquiz W/entering? onActivityResult in MainActivity
07-06 17:25:55.569 32269-32269/com.dslomer64.flagquiz W/onActResult? in Main Act show one flag Anguilla
`QuizFragment.java`:
07-06 17:25:55.591 32269-32269/com.dslomer64.flagquiz W/in showAFlag? country: Anguilla
MainActivity.java
,这是保留Preferences
并选择选项的地方
并且处理调用FlagActivity
(onActivityResult
)的结果:
@Override public boolean onOptionsItemSelected(MenuItem item){
Log.w("opOptionsItemSelected", "item is <" + item.getTitle() + "> -----------------------");
Intent preferencesIntent = new Intent(this, SettingsActivity.class);
startActivity(preferencesIntent);
Button getData = (Button)findViewById(R.id.button2);
Intent intent = new Intent(
MainActivity.this,
FlagActivity.class
);
startActivityForResult(intent, 0);
Log.w("onOptionsItemSelected", "" + item.toString() + " showFlag is " + showFlag);
return super.onOptionsItemSelected(item); // to SettingsActivity.onCreate
}
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data ) {
Log.w("entering","onActivityResult in MainActivity");
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0 && resultCode == Activity.RESULT_OK){
String enteredData = data.getStringExtra("Data");
Toast.makeText(getApplicationContext(), "Returned " + enteredData, Toast.LENGTH_SHORT).show();;
country = enteredData;
Log.w("onActResult "," in Main Act show one flag " + country);
quizFragment.showAFlag(country);
// ABOVE IS WHERE FLAG IS SHOWN ^^^
}
}
FlagActivity.java
,这是处理列表视图的地方:
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flag);
Log.w("FlagAct", " onCreate");
flagAdapter = new ArrayAdapter<String>(this, R.layout.list_item, flagArray);
setListAdapter(flagAdapter);
getListView().setOnItemClickListener(itemClickListener);
}
AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener() {
@Override public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
String country = ((TextView) view).getText().toString();
Intent result = new Intent();
result.putExtra("Data", country);
setResult(Activity.RESULT_OK, result);
Toast.makeText(getApplicationContext(), country + "!!!", Toast.LENGTH_SHORT).show();
Log.w("returnToA from FlagAct", country + "!!!");
finish();
}
}; // end itemClickListener
。
SettingsActivity.java
:
public class SettingsActivity extends Activity // hosts SettingsFragment in portrait mode
{
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.w("onCreate", "SettingsActivity");
setContentView(R.layout.activity_settings);
}
答案 0 :(得分:1)
使用Preference.OnPreferenceChangeListener
方法设置setOnPreferenceChangeListener()
。
然后,当国家/地区发生变化时,请调用finish()
方法退出您的活动
答案 1 :(得分:0)
我通过修改onOptionsItemSelected
修复了我的问题,但就我如何提问而言,我没有这样做。
showFlag = (item.toString().equals("Show flag"));
if(showFlag) // If the Settings buttonpress requested a flag,
{ // start the activity for it
Intent intent = new Intent(
MainActivity.this,
FlagActivity.class
);
startActivityForResult(intent, 0);
} // but don't start a preferences activity.
else // But if the Settings buttonpress was NOT 'Show flag',
{ // start a preferences activity.
Intent preferencesIntent = new Intent(this, SettingsActivity.class);
startActivity(preferencesIntent);
};
我仍然不知道(并且不需要)如何“解雇”Settings
屏幕。
但如果可能的话,最好知道如何做到这一点。