简体   繁体   English

在“片段活动”中实施选项菜单

[英]Implement options menu in Fragment Activity

I have two fragment activities and one fragment. 我有两个片段活动和一个片段。 Here's how the app logic looks like: 这是应用程序逻辑的样子:

FragmentActivity A ==> Fragment B ==> FragmentActivity C.

FragmentActivity A is the parent activity of fragment B and has an options menu that shows correctly.Fragment B contains a listview. FragmentActivity A是片段B的父活动,并且具有正确显示的选项菜单。片段B包含一个列表视图。 When a list item is clicked on fragment B,its details are displayed in FragmentActivity C.Now i want to display an options menu inside C in the actionBar.However, the menu is only displayed after the menu button is clicked.I want it as an actionbar action. 单击片段B上的列表项后,其详细信息将显示在FragmentActivity C中。现在我想在actionBar中的C内显示一个选项菜单,但是该菜单仅在单击菜单按钮后才显示。一个动作栏动作。

Here is the code for Activity C: 这是活动C的代码:

public class LatestDetailsFragment extends FragmentActivity implements
    OnItemClickListener {

public LatestDetailsFragment() {
    photoImages = new ImageItem();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_latestdetails);

    gallery.setOnItemClickListener(this);
    // setHasOptionsMenu(true);
}


    @Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.details_fragment_menu, menu);

    return super.onCreateOptionsMenu(menu);
}

/* *
 * Called when invalidateOptionsMenu() is triggered
 */
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    return super.onPrepareOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
    case R.id.contact:
        ContactOwnerFragment contactFragment = new ContactOwnerFragment();
        Bundle bundle = new Bundle();
        bundle.putString(KEY_PHONE, phone);
        contactFragment.setArguments(bundle);
        contactFragment.show(getSupportFragmentManager(), "contact");
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

And the menu: 和菜单:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="be.hcpl.android.example.MainActivity" >

<item
    android:id="@+id/contact"
    android:icon="@drawable/ic_action_call"
    android:orderInCategory="100"
    android:title="@string/contact"
    app:showAsAction="ifRoom|withText">
</item>

I cannot use setHasOptionsMenu(true); 我不能使用setHasOptionsMenu(true); on the activity because it raises an error,I don't know why. 关于活动,因为它会引发错误,我不知道为什么。 I want to display an icon on the actionbar. 我想在操作栏上显示一个图标。

You need to use menu.clear() before inflating menus. 您需要在menu.clear()菜单之前使用menu.clear()

@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.clear();
getMenuInflater().inflate(R.menu.details_fragment_menu, menu);
return super.onCreateOptionsMenu(menu);
}

From the Documents 来自文件

public abstract void clear () Remove all existing items from the menu, leaving it empty as if it had just been created. public abstract void clear()从菜单中删除所有现有项目,将其保留为空白,就好像它刚刚被创建一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM