2017-04-22 1 views
0

나는 appbar와 appbar가있는 fullsecreen dialogfragment (활동에서 대화 상자가 호출 됨)를 가지고 있습니다. 활동의 홈 단추를 눌렀을 때 어떤 동작을 설정했으며 대화 상자의 홈 단추를 누르면 대화 상자를 닫습니다. 두 개의 단추가 동일한 id (android.R.id.home)를 가지고 있습니다. 그리고 aparently 거기에 "onOptionsItemSelected"메서드를 호출 할 때 충돌이 있기 때문에 내가 작동하지 않는 대화 상자의 홈 단추를 누르지 만, 활동에 코드의 부분을 제거하는 경우 (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); 

}