2017-04-22 13 views
0

私はappbarとappbar(アクティビティから呼び出されるダイアログ)を持つfullsecreenダイアログフラグメントを持っています。 アクティビティのホームボタンが押されたときに何らかのアクションを設定しました。ダイアログのホームボタンが押されると、ダイアログが閉じます。 私は、2つのボタンが同じid(android.R.id.home)を持っていることに気付きました。どうやら、メソッドの "onOptionsItemSelected"が呼び出されたときに競合が発生するのは、ダイアログのホームボタンを押しても機能しないためですが、アクティビティのコード部分を削除すると(if id == android.R .id.home) 正常に動作し、ダイアログが閉じます。 どうすればよいですか。この競合を防ぐ方法がありますか、ホームボタンのIDを別に設定するのでしょうか?アクティビティのホームボタンとDialogfragmentのホームボタンのIDとの間の競合

が、これは、これはあなたが活動中で、このソリューションを適用して試すことができますdialogfragmentの方法

public boolean onOptionsItemSelected(MenuItem item) { 
     int id = item.getItemId(); 

     if (id == R.id.action_next) { 
      pager.setCurrentItem(pager.getCurrentItem() + 1); 
      updateTitle(); 
      return true; 
     } else if (id==R.id.action_previous) 
     { 

      pager.setCurrentItem(pager.getCurrentItem() - 1); 
      updateTitle(); 
      return true; 
     } 

     else if (id == android.R.id.home) { 

      dismiss(); 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

答えて

0

ある

public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_logout) { 
      exitApp(); 
      return true; 
     } 

     else if ((id == android.R.id.home) && searchActivated) { 
      drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED); 
      toggle.syncState(); 
      searchActivated=false; 
      reload_fragment_data(); 
      return true; 
     } 
     else if ((id == android.R.id.home) && (!searchActivated)) 
     { 
      drawer.openDrawer(GravityCompat.START); // OPEN DRAWER 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 

    } 

活動の方法です。

public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    Fragment fragment = getFragmentManager().findFragmentById(R.id.fragment_container); 
    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_logout) { 
     exitApp(); 
     return true; 
    } 

    else if ((id == android.R.id.home) && searchActivated && !(fragment instanceof DialogFragmentClassName)) { 
     drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED); 
     toggle.syncState(); 
     searchActivated=false; 
     reload_fragment_data(); 
     return true; 
    } 
    else if ((id == android.R.id.home) && (!searchActivated)) 
    { 
     drawer.openDrawer(GravityCompat.START); // OPEN DRAWER 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 

} 
関連する問題