我有一个AppCompatActivity和一些片段. *** 作栏上的后退按钮在AboutFragment中不起作用.
活动没有菜单膨胀,而片段只在 *** 作栏中有一个后退按钮.
片段菜单可见,但后退按钮在点击时根本不作出反应.
点击 *** 作栏上的信息图标即可显示AboutFragment.
单击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里面的后退按钮不会调用Fragment中的onoptionsItemSelected()方法.
在调试时,我可以看到调用Fragment和Activity的onCreateOptionsMenu(),但是当点击按钮时,不会调用onoptionsItemSelected(),也不会调用来自Fragment的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 likeLog.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); }*/}
AboutFragment
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(); }}
这是我的AboutFragment xml代码
<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>
解决方法:
您可以直接在工具栏上设置OnMenuItemClickListener的实例.请记住,您不应该使用Activity.setSupportActionbar(工具栏).
public class AboutFragment extends Fragment { // ... @OverrIDe public VIEw onCreateVIEw(LayoutInflater inflater, VIEwGroup container, Bundle savedInstanceState) { VIEw rootVIEw = inflater.inflate(R.layout.about_layout, container, false); toolbar = (Toolbar) rootVIEw.findVIEwByID(R.ID.toolbar); toolbar.inflateMenu(R.menu.menu_about_fragment); toolbar.setonMenuItemClickListener(new Toolbar.OnMenuItemClickListener() { @OverrIDe public boolean onMenuItemClick(MenuItem item) { if (item.getItemID() == R.ID.some_menu) { // do sth... return true; // event is handled. } return false; } }); // ... } // ...}
.
更新21/11/2018:
要为独立工具栏启用后退按钮,请使用app:navigationIcon =“?attr / homeAsUpIndicator”在xml文件中设置其默认图标:
<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" app:navigationIcon="?attr/homeAsUpIndicator" />
然后为它设置一个监听器
toolbar.setNavigationOnClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { // back button is pressed mListener.onHomepressed(); }});
总结 以上是内存溢出为你收集整理的java – onOptionsItemSelected()方法不会在Fragment上调用全部内容,希望文章能够帮你解决java – onOptionsItemSelected()方法不会在Fragment上调用所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)