2017-11-01 3 views
0

onBackPressed 함수를 재정의하여 현재 작업중인 앱의 뒤쪽 키의 동작을 변경합니다..remove (프래그먼트)를 호출 한 후 프래그먼트가 삭제되지 않습니다.

FragmentTransaction에 특정 태그 qFrag이 포함 된 조각이 포함 된 경우보기에서 조각을 애니메이션화해야합니다.

그러나 조각을 제거한 후에도 조각은 여전히 ​​나타납니다. 이 때문에 내 if 문의 조건 인 else에 결코 도달하지 못합니다.

@Override 
public void onBackPressed() { 
    // After pressing the back key a second time, questionFrag still has a value. 
    Fragment questionFrag = getSupportFragmentManager().findFragmentByTag("qFrag"); 

    // If question fragment in the fragment manager 
    if (questionFrag != null) { 
     getSupportFragmentManager() 
       .beginTransaction() 
       .setCustomAnimations(R.anim.slide_up, R.anim.slide_down) 
       .replace(R.id.main_menu_content_frame_layout, new Fragment(), "temp") 
       .remove(questionFrag) 
       .commit(); 

    } else { 
     finish(); 
    } 

} 

누군가가 트랜잭션 관리자에서 조각을 적절하게 제거하는 방법에 대해 조언 해 줄 수 있습니까?

if (questionFrag != null) { 
    getSupportFragmentManager() 
      .beginTransaction() 
      .remove(questionFrag) 
      .add(R.id.main_menu_content_frame_layout, new Fragment(), "temp") 
      .setCustomAnimations(R.anim.slide_up, R.anim.slide_down) 
      .commit(); 

      System.out.println("questionFrag removed successfully"); 

      finish() 
} else { 
    finish(); 
} 

당신은 System.out에를 추가 할 수 있습니다

답변

1

이 주제에 조각에 대해 알아야 할 두 가지가 있습니다

1 - 당신이 조각에 태그를 줄 때, 그것은 당신이 가장 확실하게있는 스택을 다시 추가,이 :

Fragment questionFrag = getSupportFragmentManager().findFragmentByTag("qFrag"); 

popBackStack()으로 전화하는 경우에만 null을 반환합니다. 당신이 그것을 호출하지 않기 때문에 당신이 그것을 제거한다고하더라도 조각은 여전히 ​​백 스택에 저장됩니다.

2 - 당신이 replace 당신이 주어진 컨테이너에있는 모든 조각을 제거하고 새로운 하나를 추가 호출 호출 할 때부터

.replace(R.id.main_menu_content_frame_layout, new Fragment(), "temp") 
.remove(questionFrag) 

이 redundat입니다 코드의이 비트 그래서이

.replace(R.id.main_menu_content_frame_layout, new Fragment(), "temp") 

이면 충분합니다. 후 교체

가 다시 스택을 저런 애 : 대신에 가기 backstack을 저런 애의

if (questionFrag != null) { 
    getSupportFragmentManager() 
     .beginTransaction() 
     .replace(R.id.main_menu_content_frame_layout, new Fragment(), "temp") 
     .setCustomAnimations(R.anim.slide_up, R.anim.slide_down) 
     .commit(); 

    getSupportFragmentManager().popBackStack();  

    System.out.println("questionFrag removed successfully"); 

    finish() 
} else { 
    finish(); 
} 

또는 조각이있는 경우 확인, 두 가지 문제를 해결할 너무 방법이 이제

, 컨테이너

Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.main_menu_content_frame_layout); 

if(fragment instanceOf QuestionFragment){ 
    getSupportFragmentManager() 
     .beginTransaction() 
     .replace(R.id.main_menu_content_frame_layout, new Fragment(), "temp") 
     .setCustomAnimations(R.anim.slide_up, R.anim.slide_down) 
     .commit(); 

    getSupportFragmentManager().popBackStack();  

    System.out.println("questionFrag removed successfully"); 

    finish() 

} else { 
    finish(); 
} 
+1

감사합니다. 주제에 어려움을 겪어 왔지만 이것이 분명 도움이됩니다. – Airagale

0

(.remove가 컨테이너를 비워 때문에, 대체하지) 다음 새 조각을 추가, 첫 .remove 호출이 시도하고 사용자 지정 애니메이션을 설정 코드가 어느 지점에 도달했는지 확인하기 위해 오류를 찾는 좋은 방법입니다. 로그에 인쇄물이 보이지 않으면 FragmentManager 코드가 제대로 실행되고 있지 않음을 알 수 있습니다.

관련 문제