使用onOptionsItemSelected从PreferenceArag和PreferenceFragments上升 [英] Using onOptionsItemSelected to go up from PreferenceActivity with PreferenceFragments
问题描述
我在使用Android Studio的默认设置活动(扩展AppCompatPreferenceActivity
)时使用 onOptionsItemSelected
时遇到问题。活动的重要部分是:
I'm having trouble using onOptionsItemSelected
with Android Studio's default "Settings Activity" (which extends AppCompatPreferenceActivity
). The important parts of the activity are:
public class SettingsActivity extends AppCompatPreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setupActionBar();
}
private void setupActionBar() {
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
// Show the Up button in the action bar.
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
public static class GeneralPreferenceFragment extends PreferenceFragment {
// [...]
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
startActivity(new Intent(getActivity(), SettingsActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
}
// two more fragments
}
这适用于片段 - onOptionsItemSelected
@Override
运行良好,返回到 SettingsActivity
,但我希望 SettingsActivity
在使用向上按钮时将控制权返回到其父活动。
This works great for the fragments -- the onOptionsItemSelected
@Override
s work well, returning to SettingsActivity
, but I want SettingsActivity
to return control to its parent activity when the up button is used.
我读过文档,我理解
虽然您的片段收到每个菜单项的项目选择回调,但它添加,当用户选择菜单项时,活动首先接收相应的回调。
Although your fragment receives an on-item-selected callback for each menu item it adds, the activity is first to receive the respective callback when the user selects a menu item.
这意味着我不能简单地添加一个类似的 @Override
(使用不同的 Intent
)到 SettingsActivity
本身处理我想要的情况,以免碎片重新调整转而使用父活动。
This means I can't simply add a similar @Override
(with a different Intent
) to SettingsActivity
itself to handle the case I want to, lest the fragments return to the parent activity instead.
我试图通过 AndroidManifest.xml
来处理这个问题:
I've attempted handling this via AndroidManifest.xml
:
<activity
android:name=".SettingsActivity"
android:label="@string/title_activity_settings"
android:parentActivityName=".BlahBlahActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.appthing.BlahBlahActivity" />
</activity>
但这似乎没有做任何事情。
but this doesn't seem to do anything at all.
处理这个问题的好方法是什么?
What's a good way, if any, to deal with this?
推荐答案
在Android创建的模板中Studio onOptionsItemSelected方法属于片段。
In the template created by Android Studio onOptionsItemSelected methods belong to the fragments.
我通过评论它们并为覆盖onBackPressed的活动编写onOptionsItemSelected方法解决了这个问题
I solved by commenting them and writing onOptionsItemSelected method for the activity overriding onBackPressed
@Override
public void onBackPressed() {
super.onBackPressed();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
这篇关于使用onOptionsItemSelected从PreferenceArag和PreferenceFragments上升的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!