Java 未对片段调用OnOptions ItemSelected()方法

Java 未对片段调用OnOptions ItemSelected()方法,java,android,android-fragments,android-actionbar,Java,Android,Android Fragments,Android Actionbar,我有一个活动和一些片段。操作栏上的“后退”按钮在AboutFragment中不起作用 活动没有菜单,而片段在操作栏中只有一个后退按钮 片段菜单可见,但点击后退按钮时根本没有反应 单击actionbar中的信息图标,将显示关于片段的 单击i图标时,下面的方法在MainActivity中工作 @Override public void onInfoSelected() { abf = (AboutFragment) getSupportFragmentManager()

我有一个活动和一些片段。操作栏上的“后退”按钮在
AboutFragment
中不起作用

活动没有菜单,而片段在操作栏中只有一个后退按钮

片段菜单可见,但点击后退按钮时根本没有反应

单击actionbar中的信息图标,将显示关于片段的

单击
i
图标时,下面的方法在
MainActivity
中工作

 @Override
    public void onInfoSelected() {

        abf = (AboutFragment) getSupportFragmentManager().findFragmentByTag(ABOUT_FRAGMENT_TAG);

        FragmentTransaction ft = fm.beginTransaction();
        ft.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right,
                android.R.anim.slide_in_left, android.R.anim.slide_out_right);
       // ft.hide(pf);

        //ft.hide(cf);
        ft.hide(tlf);
        ft.show(abf);
        ft.addToBackStack(null);
        ft.commit();
    }
然后AboutFragment内的back按钮不会调用片段内的
OnOptions ItemSelected()
方法

调试时,我可以看到片段和活动的
onCreateOptionsMenu()
都被调用,但当点击按钮时,无论是从活动还是从片段,都不会调用任何
onOptionsItemSelected()

这两天我一直在搜索和谷歌搜索,什么都没用。 任何帮助都将受到感谢

活动Java代码

package me.declangao.jiasazsales.app;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import me.declangao.jiasazsales.R;
import me.declangao.jiasazsales.model.Post;

public class MainActivity extends AppCompatActivity implements
        RecyclerViewFragment.PostListListener, PostFragment.PostListener,
        TabLayoutFragment.TabLayoutListener, SearchResultFragment.SearchResultListener,
        CommentFragment.CommentListener,AboutFragment.AboutListener {

    private static final String TAG = MainActivity.class.getSimpleName();
    public static final String TAB_LAYOUT_FRAGMENT_TAG = "TabLayoutFragment";
    public static final String POST_FRAGMENT_TAG = "PostFragment";
    public static final String COMMENT_FRAGMENT_TAG = "CommentFragment";

    public static final String ABOUT_FRAGMENT_TAG = "AboutFragment";


    private FragmentManager fm = null;
    private TabLayoutFragment tlf;
    private PostFragment pf;
    private CommentFragment cf;
    private SearchResultFragment srf;
    private AboutFragment abf;


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



        fm = getSupportFragmentManager();

        // Setup fragments
        tlf = new TabLayoutFragment();
        pf = new PostFragment();
        cf = new CommentFragment();
        srf = new SearchResultFragment();
        abf=new AboutFragment();
        FragmentTransaction ft = fm.beginTransaction();
        ft.add(android.R.id.content, abf, ABOUT_FRAGMENT_TAG);
        ft.add(android.R.id.content, pf, POST_FRAGMENT_TAG);
        ft.add(android.R.id.content, cf, COMMENT_FRAGMENT_TAG);
        ft.add(android.R.id.content, tlf, TAB_LAYOUT_FRAGMENT_TAG);


        ft.hide(pf);
        ft.hide(cf);
        ft.hide(abf);
        ft.show(tlf);
        ft.commit();
    }

    /**
     * Invoked when a post in the list is selected
     *
     * @param post Selected Post object
     */
    @Override
    public void onPostSelected(Post post, boolean isSearch) {
        // Find the fragment in order to set it up later
        pf = (PostFragment) getSupportFragmentManager().findFragmentByTag(POST_FRAGMENT_TAG);

        // Set necessary arguments
        Bundle args = new Bundle();
        args.putInt("id", post.getId());
        args.putString("title", post.getTitle());
        args.putString("date", post.getDate());
        args.putString("author", post.getAuthor());
        args.putString("content", post.getContent());
        args.putString("url", post.getUrl());
        //args.putString("thumbnailUrl", post.getThumbnailUrl());
        args.putString("featuredImage", post.getFeaturedImageUrl());

        // Configure PostFragment to display the right post
        pf.setUIArguments(args);

        // Show the fragment
        FragmentTransaction ft = fm.beginTransaction();
        ft.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right,
                android.R.anim.slide_in_left, android.R.anim.slide_out_right);
        if (!isSearch) { // Hide TabLayoutFragment if this is not search result
            ft.hide(tlf);

        } else { // Otherwise, hide the search result, ie. SearchResultFragment.
            ft.hide(srf);
        }
        ft.show(pf);
        ft.addToBackStack(null);
        ft.commit();
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                //do something here like
Log.e("menu Test","rom Main activity");
                int backStackEntryCount
                        =getSupportFragmentManager().getBackStackEntryCount();

                if (backStackEntryCount > 0) {

                    getSupportFragmentManager().popBackStack();

                }

                return true;
        }
        return false;
    }
    /**
     * Invoked when a search query is submitted
     *
     * @param query Selected Post object
     */
    @Override
    public void onSearchSubmitted(String query) {
        FragmentTransaction ft = fm.beginTransaction();
        ft.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right,
                android.R.anim.slide_in_left, android.R.anim.slide_out_right);

        // Send query to fragment using factory method
        srf = SearchResultFragment.newInstance(query);
        ft.add(android.R.id.content, srf);
        ft.hide(tlf);
        ft.addToBackStack(null);
        ft.commit();
    }


    @Override
    public void onInfoSelected() {

        abf = (AboutFragment) getSupportFragmentManager().findFragmentByTag(ABOUT_FRAGMENT_TAG);

        FragmentTransaction ft = fm.beginTransaction();
        ft.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right,
                android.R.anim.slide_in_left, android.R.anim.slide_out_right);
       // ft.hide(pf);

        //ft.hide(cf);
        ft.hide(tlf);
        ft.show(abf);
        ft.addToBackStack(null);
        ft.commit();
    }
    /**
     * Invoked when comment menu is selected
     *
     * @param id ID of the article, assigned by WordPress
     */
    @Override
    public void onCommentSelected(int id) {
        cf = (CommentFragment) getSupportFragmentManager().findFragmentByTag(COMMENT_FRAGMENT_TAG);
        Bundle args = new Bundle();
        args.putInt("id", id);
        // Setup CommentFragment to display the right comments page
        cf.setUIArguments(args);

        FragmentTransaction ft = fm.beginTransaction();
        ft.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right,
                android.R.anim.slide_in_left, android.R.anim.slide_out_right);
        ft.hide(pf);
        //ft.hide(abf);
        ft.show(cf);
        ft.addToBackStack(null);
        ft.commit();
    }

    /**
     * Intercept back button event, reset ActionBar if necessary
     */
    @Override
    public void onBackPressed() {
        resetActionBarIfApplicable();
        super.onBackPressed();
    }

    /**
     * Simulate a back button press when home is selected
     */
    @Override
    public void onHomePressed() {
        resetActionBarIfApplicable();
        fm.popBackStack();
    }

    /**
     * Reset TabLayoutFragment's ActionBar if necessary
     */
    private void resetActionBarIfApplicable() {
        Log.d(TAG, "SearchResultFragment is visible: " + srf.isHidden());
        if (srf.isVisible()) {
            tlf.resetActionBar();
        }
    }

    // Commented out coz we will let fragments handle their own Options Menus
/*
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        //Log.e("Erroraaa","aaaaa");

        return true;
    }



    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        Log.e("menu","activity: action home has clicked");
        switch (item.getItemId()){
            case android.R.id.home:

                onBackPressed();
                return false;

        }

        return super.onOptionsItemSelected(item);
    }
*/

}
<FrameLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorAccent"
    android:layoutDirection="rtl"
    android:textDirection="rtl"

    tools:context="me.declangao.jiasazsales.app.AboutFragment">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layoutDirection="rtl"
        android:textDirection="rtl"
        android:orientation="vertical">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/colorPrimary"
            android:theme="@style/ThemeOverlay.AppCompat.Dark"
            android:layoutDirection="rtl"
            android:textDirection="rtl"
            />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layoutDirection="rtl"
        android:textDirection="rtl"
        android:padding="15dp"
        android:orientation="vertical">

        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_marginBottom="10dp"
            android:text="ئه‌م ئاپه‌ له‌لایه‌ن كۆمپانیای جیاساز دروست كراوه‌"
            android:textAppearance="@style/TextAppearance.AppCompat.Light.SearchResult.Title" />

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="match_parent"
            android:layout_height="214dp"
            android:src="@drawable/jiasazlogo" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_marginBottom="10dp"
            android:text="كۆمپانیای جیاساز بۆ خزمه‌تگوزاری و چاره‌سه‌ری ته‌كنه‌لۆجی، دروستكردنی وێبسایت و ئاپی مۆبایل و سیسته‌می دام و ده‌زگاكان و ماركێته‌كان"
            android:textAppearance="@style/TextAppearance.AppCompat.Light.SearchResult.Title" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:autoLink="all"
            android:clickable="true"
            android:text="@string/link"
            android:textAlignment="center"
            android:textAppearance="@style/TextAppearance.AppCompat.Body1"
            android:textSize="18sp" />

    </LinearLayout>
    </LinearLayout>

</FrameLayout>
关于碎片

 package me.declangao.jiasazsales.app;

import android.app.Activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import me.declangao.jiasazsales.R;

/**
 * Fragment to display a Info about Jiasaz company.
 * Activities that contain this fragment must implement the
 * {@link AboutFragment.AboutListener} interface
 * to handle interaction events.
 */
public class AboutFragment extends Fragment {



    private AboutListener mListener;
    private Toolbar toolbar;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setRetainInstance(true);

        this.setHasOptionsMenu(true);

    }

//    @Override
//    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
//        Log.e("Menu:created","Menu");
//
////        super.onCreateOptionsMenu(menu, inflater);
////        menu.clear();
//        inflater.inflate(R.menu.menu_post, menu);
//        super.onCreateOptionsMenu(menu, inflater);
//    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Log.e("Menu:selected","Menu");
        if (item.getItemId() == android.R.id.home) {
            mListener.onHomePressed();
        }
        return false;
    }


    public AboutFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.about_layout, container, false);

        toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
        ((MainActivity) getActivity()).setSupportActionBar(toolbar);
        ((MainActivity) getActivity()).getSupportActionBar().setHomeButtonEnabled(true);
        ((MainActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        ((MainActivity) getActivity()).getSupportActionBar().setTitle(  "www.Jiasaz.com");
         Log.e("Menu: fragment created","onCreaate()");

        return rootView;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (AboutListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement AboutListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p/>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface AboutListener {
       void onHomePressed();
        //void onInfoSelected();
    }




}
package me.declangao.jiasazsales.app;
导入android.app.Activity;
导入android.os.Bundle;
导入android.support.v4.app.Fragment;
导入android.support.v4.app.FragmentTransaction;
导入android.support.v7.widget.Toolbar;
导入android.util.Log;
导入android.view.LayoutInflater;
导入android.view.Menu;
导入android.view.MenuInflater;
导入android.view.MenuItem;
导入android.view.view;
导入android.view.ViewGroup;
进口me.declangao.jiasazsales.R;
/**
*片段以显示有关Jiasaz公司的信息。
*包含此片段的活动必须实现
*{@link AboutFragment.AboutListener}接口
*处理交互事件。
*/
公共类AboutFragment扩展了Fragment{
关于Listener mListener的私人信息;
专用工具栏;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
此.setRetainInstance(真);
此.sethasOptions菜单(true);
}
//@覆盖
//创建选项菜单(菜单菜单,菜单充气机){
//Log.e(“菜单:已创建”、“菜单”);
//
////super.onCreateOptions菜单(菜单,充气机);
////menu.clear();
//充气机。充气(右菜单。菜单栏,菜单栏);
//super.onCreateOptions菜单(菜单,充气机);
//    }
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
Log.e(“菜单:已选择”、“菜单”);
if(item.getItemId()==android.R.id.home){
mListener.onHomePressed();
}
返回false;
}
公众关于碎片(){
//必需的空公共构造函数
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
//为该碎片膨胀布局
视图根视图=充气机。充气(R.layout.about_布局,容器,false);
toolbar=(toolbar)rootView.findviewbyd(R.id.toolbar);
((MainActivity)getActivity()).setSupportActionBar(工具栏);
((MainActivity)getActivity()).getSupportActionBar().setHomeButtonEnabled(true);
((MainActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
((MainActivity)getActivity()).getSupportActionBar().setTitle(“www.Jiasaz.com”);
e(“菜单:创建的片段”,“oncreate()”);
返回rootView;
}
@凌驾
公共事务主任(活动){
超级转速计(活动);
试一试{
mListener=(AboutListener)活动;
}catch(ClassCastException e){
抛出新的ClassCastException(activity.toString()
+“必须实施AboutListener”);
}
}
@凌驾
公共无效连接(){
super.onDetach();
mListener=null;
}
/**
*此接口必须由包含以下内容的活动实现
*片段,以允许通信此片段中的交互
*该活动以及其中可能包含的其他片段
*活动。
*

*有关更多信息,请参阅Android培训课程。 */ 关于Listener的公共接口{ void onHomePressed(); //void oninfo selected(); } }

这是我的AboutFragment xml代码

package me.declangao.jiasazsales.app;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import me.declangao.jiasazsales.R;
import me.declangao.jiasazsales.model.Post;

public class MainActivity extends AppCompatActivity implements
        RecyclerViewFragment.PostListListener, PostFragment.PostListener,
        TabLayoutFragment.TabLayoutListener, SearchResultFragment.SearchResultListener,
        CommentFragment.CommentListener,AboutFragment.AboutListener {

    private static final String TAG = MainActivity.class.getSimpleName();
    public static final String TAB_LAYOUT_FRAGMENT_TAG = "TabLayoutFragment";
    public static final String POST_FRAGMENT_TAG = "PostFragment";
    public static final String COMMENT_FRAGMENT_TAG = "CommentFragment";

    public static final String ABOUT_FRAGMENT_TAG = "AboutFragment";


    private FragmentManager fm = null;
    private TabLayoutFragment tlf;
    private PostFragment pf;
    private CommentFragment cf;
    private SearchResultFragment srf;
    private AboutFragment abf;


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



        fm = getSupportFragmentManager();

        // Setup fragments
        tlf = new TabLayoutFragment();
        pf = new PostFragment();
        cf = new CommentFragment();
        srf = new SearchResultFragment();
        abf=new AboutFragment();
        FragmentTransaction ft = fm.beginTransaction();
        ft.add(android.R.id.content, abf, ABOUT_FRAGMENT_TAG);
        ft.add(android.R.id.content, pf, POST_FRAGMENT_TAG);
        ft.add(android.R.id.content, cf, COMMENT_FRAGMENT_TAG);
        ft.add(android.R.id.content, tlf, TAB_LAYOUT_FRAGMENT_TAG);


        ft.hide(pf);
        ft.hide(cf);
        ft.hide(abf);
        ft.show(tlf);
        ft.commit();
    }

    /**
     * Invoked when a post in the list is selected
     *
     * @param post Selected Post object
     */
    @Override
    public void onPostSelected(Post post, boolean isSearch) {
        // Find the fragment in order to set it up later
        pf = (PostFragment) getSupportFragmentManager().findFragmentByTag(POST_FRAGMENT_TAG);

        // Set necessary arguments
        Bundle args = new Bundle();
        args.putInt("id", post.getId());
        args.putString("title", post.getTitle());
        args.putString("date", post.getDate());
        args.putString("author", post.getAuthor());
        args.putString("content", post.getContent());
        args.putString("url", post.getUrl());
        //args.putString("thumbnailUrl", post.getThumbnailUrl());
        args.putString("featuredImage", post.getFeaturedImageUrl());

        // Configure PostFragment to display the right post
        pf.setUIArguments(args);

        // Show the fragment
        FragmentTransaction ft = fm.beginTransaction();
        ft.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right,
                android.R.anim.slide_in_left, android.R.anim.slide_out_right);
        if (!isSearch) { // Hide TabLayoutFragment if this is not search result
            ft.hide(tlf);

        } else { // Otherwise, hide the search result, ie. SearchResultFragment.
            ft.hide(srf);
        }
        ft.show(pf);
        ft.addToBackStack(null);
        ft.commit();
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                //do something here like
Log.e("menu Test","rom Main activity");
                int backStackEntryCount
                        =getSupportFragmentManager().getBackStackEntryCount();

                if (backStackEntryCount > 0) {

                    getSupportFragmentManager().popBackStack();

                }

                return true;
        }
        return false;
    }
    /**
     * Invoked when a search query is submitted
     *
     * @param query Selected Post object
     */
    @Override
    public void onSearchSubmitted(String query) {
        FragmentTransaction ft = fm.beginTransaction();
        ft.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right,
                android.R.anim.slide_in_left, android.R.anim.slide_out_right);

        // Send query to fragment using factory method
        srf = SearchResultFragment.newInstance(query);
        ft.add(android.R.id.content, srf);
        ft.hide(tlf);
        ft.addToBackStack(null);
        ft.commit();
    }


    @Override
    public void onInfoSelected() {

        abf = (AboutFragment) getSupportFragmentManager().findFragmentByTag(ABOUT_FRAGMENT_TAG);

        FragmentTransaction ft = fm.beginTransaction();
        ft.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right,
                android.R.anim.slide_in_left, android.R.anim.slide_out_right);
       // ft.hide(pf);

        //ft.hide(cf);
        ft.hide(tlf);
        ft.show(abf);
        ft.addToBackStack(null);
        ft.commit();
    }
    /**
     * Invoked when comment menu is selected
     *
     * @param id ID of the article, assigned by WordPress
     */
    @Override
    public void onCommentSelected(int id) {
        cf = (CommentFragment) getSupportFragmentManager().findFragmentByTag(COMMENT_FRAGMENT_TAG);
        Bundle args = new Bundle();
        args.putInt("id", id);
        // Setup CommentFragment to display the right comments page
        cf.setUIArguments(args);

        FragmentTransaction ft = fm.beginTransaction();
        ft.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right,
                android.R.anim.slide_in_left, android.R.anim.slide_out_right);
        ft.hide(pf);
        //ft.hide(abf);
        ft.show(cf);
        ft.addToBackStack(null);
        ft.commit();
    }

    /**
     * Intercept back button event, reset ActionBar if necessary
     */
    @Override
    public void onBackPressed() {
        resetActionBarIfApplicable();
        super.onBackPressed();
    }

    /**
     * Simulate a back button press when home is selected
     */
    @Override
    public void onHomePressed() {
        resetActionBarIfApplicable();
        fm.popBackStack();
    }

    /**
     * Reset TabLayoutFragment's ActionBar if necessary
     */
    private void resetActionBarIfApplicable() {
        Log.d(TAG, "SearchResultFragment is visible: " + srf.isHidden());
        if (srf.isVisible()) {
            tlf.resetActionBar();
        }
    }

    // Commented out coz we will let fragments handle their own Options Menus
/*
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        //Log.e("Erroraaa","aaaaa");

        return true;
    }



    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        Log.e("menu","activity: action home has clicked");
        switch (item.getItemId()){
            case android.R.id.home:

                onBackPressed();
                return false;

        }

        return super.onOptionsItemSelected(item);
    }
*/

}
<FrameLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorAccent"
    android:layoutDirection="rtl"
    android:textDirection="rtl"

    tools:context="me.declangao.jiasazsales.app.AboutFragment">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layoutDirection="rtl"
        android:textDirection="rtl"
        android:orientation="vertical">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/colorPrimary"
            android:theme="@style/ThemeOverlay.AppCompat.Dark"
            android:layoutDirection="rtl"
            android:textDirection="rtl"
            />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layoutDirection="rtl"
        android:textDirection="rtl"
        android:padding="15dp"
        android:orientation="vertical">

        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_marginBottom="10dp"
            android:text="ئه‌م ئاپه‌ له‌لایه‌ن كۆمپانیای جیاساز دروست كراوه‌"
            android:textAppearance="@style/TextAppearance.AppCompat.Light.SearchResult.Title" />

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="match_parent"
            android:layout_height="214dp"
            android:src="@drawable/jiasazlogo" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_marginBottom="10dp"
            android:text="كۆمپانیای جیاساز بۆ خزمه‌تگوزاری و چاره‌سه‌ری ته‌كنه‌لۆجی، دروستكردنی وێبسایت و ئاپی مۆبایل و سیسته‌می دام و ده‌زگاكان و ماركێته‌كان"
            android:textAppearance="@style/TextAppearance.AppCompat.Light.SearchResult.Title" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:autoLink="all"
            android:clickable="true"
            android:text="@string/link"
            android:textAlignment="center"
            android:textAppearance="@style/TextAppearance.AppCompat.Body1"
            android:textSize="18sp" />

    </LinearLayout>
    </LinearLayout>

</FrameLayout>

第一个原因是您没有正确地实现
onCreateOptions菜单()
onOptions ItemSelected()
@覆盖
活动中的方法
,因此取消了
活动
中的注释,这样
活动
也可以调用它们各自的
超级
方法

第二个原因是您的
onCreateOptions菜单()
代码被注释了,这需要
而不是
。重要的是super
super.onCreateOptions菜单(菜单,充气机)来绘制
片段的菜单

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
}
并将
Fragment
中的选项ItemSelected()
更改为:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    Log.e("Menu:selected","Menu");
    if (item.getItemId() == android.R.id.home) {
        mListener.onHomePressed();
        return true;
    }else{
        return super.onOptionsItemSelected(item);
    }

}
@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return super.onCreateOptionsMenu(menu);
    }
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    return super.onOptionsItemSelected(item);
}
现在在
活动中
将您的
onCreateOptions菜单()更改为:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    Log.e("Menu:selected","Menu");
    if (item.getItemId() == android.R.id.home) {
        mListener.onHomePressed();
        return true;
    }else{
        return super.onOptionsItemSelected(item);
    }

}
@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return super.onCreateOptionsMenu(menu);
    }
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    return super.onOptionsItemSelected(item);
}
onOptionsItemSelected()

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    Log.e("Menu:selected","Menu");
    if (item.getItemId() == android.R.id.home) {
        mListener.onHomePressed();
        return true;
    }else{
        return super.onOptionsItemSelected(item);
    }

}
@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return super.onCreateOptionsMenu(menu);
    }
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    return super.onOptionsItemSelected(item);
}

您正确地描述了一切,这些都是平台的局限性。要解决此问题,您需要使用onOptions ItemSelected() 在MainActivity中,插入已在其他方法中使用的代码:

abf = (AboutFragment) getSupportFragmentManager().findFragmentByTag(ABOUT_FRAGMENT_TAG);

然后,当您拥有片段时,您可以从片段中调用一些方法,并可能获得返回值,以了解是否处理了背部,以及是否需要执行更多操作…

您可以在片段类中使用
onPrepareOptionsMenu(Menu Menu)
来侦听操作栏菜单

@Override
public void onPrepareOptionsMenu(Menu menu)
{
    super.onPrepareOptionsMenu(menu);

    // to get an item of menu from action bar you can do it like...
    // this below line will find an item of menu with id action_search...
    MenuItem searchMenuItem = menu.findItem(R.id.action_search);

    // like this you can find other menu items
    // another example is
    MenuItem infoMenuItem = menu.findItem(R.id.action_info);
}
我正在使用中的搜索视图