簡體   English   中英

如何在單擊“主頁”按鈕時提供適當的反向導航以及片段

[英]how to provide proper back navigation with fragment on clicking the home button

我想在單擊操作欄的“主頁”按鈕時提供向后導航。 在主機片段中

((MainActivity)this.getActivity()).fragment_Actualités=new Fragment_Actualités();
         fragmentTransaction.replace(R.id.content_frame,((MainActivity)this.getActivity()).fragment_Actualités);
         fragmentTransaction.addToBackStack(null);
         fragmentTransaction.commit();
        ((MainActivity)this.getActivity()).fragmentManager.executePendingTransactions();
        ((MainActivity)this.getActivity()).supportInvalidateOptionsMenu();

在主持人活動中

  public boolean onOptionsItemSelected(MenuItem item) {
    // The action bar home/up action should open or close the drawer.
    // ActionBarDrawerToggle will take care of this.
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    // Handle action buttons
    int id=0;
    if(id==item.getItemId()){
         if(id== R.id.home)
        {
            this.onBackPressed();
            return true;}

        }

當我單擊后退按鈕時,super.OnBackPressed可以正常工作,如何按預期方式返回

嘗試更改:

this.onBackPressed();

至:

onBackPressed();

並擴展FragmentActivity(如果您還沒有這樣做的話)

再次閱讀代碼,我認為找到了問題,您為id分配了0,因此當您使用item.getItemId()時,始終返回false。 嘗試使用此代碼

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int itemId = item.getItemId();
    if (itemId == android.R.id.home) {
        this.onBackPressed();
        return true;
    }
}

我終於解決了,問題是,我有2個MenuItems,Settings和Home One's。 因此,如果僅編寫R.id.Home ,它將無法正常工作,但是當更改為android.R.id.Home時,它將正常工作! 用相同的代碼

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM