2016-06-16 3 views
1

나는 조각으로 사용하는 4 자바 클래스를 가지고 :`F1, F2, F3, F4. 다음으로 내가 이전에 그들 중 하나에서 반환 할 수 있도록하려면안드로이드 현재 조각 찾기

android.app.FragmentManager FM = getFragmentManager(); 
     FragmentTransaction FT = FM.beginTransaction(); 
     F1 FP = new F1(); 
     FT.add(R.id.where, FP,"F1"); 
     FT.commit(); 
     FM.executePendingTransactions(); 

, 난 addToBackStack(TAG) 추가 : 내가 다른 하나에서 Switch가 아래로 내려가 할 때 , 난이 코드를 사용

android.app.FragmentManager FM = getFragmentManager(); 
     FragmentTransaction FT = FM.beginTransaction(); 
     F1 FP = new F1(); 
     FT.add(R.id.where, FP,"F1"); 
     FT.addToBackStack("F1"); 
     FT.commit(); 
     FM.executePendingTransactions(); 

표시되는 현재 조각을 변경하는 활동이 하나뿐입니다. 나는 그것으로부터 얻을 것이고, 현재 조각이 표시 될 것이다. 이 글을 쓰려고했지만 now은 항상 null입니다.

Fragment now=getSupportFragmentManager().findFragmentByTag("F1"); 
if(now!=null && now.isVisible()) { 
//some code for the current fragment 
} 

답변

6

당신

Fragment currentFragment = getFragmentManager().findFragmentById(R.id.where); 
if (currentFragment instanceof F1) { 
     //do your stuff here 
} 
에 도움이 될 수 있습니다 이것 봐