2013-01-22 1 views
0

이것은 쉽게 생각할 수 있지만 생각보다 어렵습니다. 두 단편이 언제 내 조각 중 하나에서 화면에 표시되는지 확인하고 싶습니다. onCreate이 가장 적합한 장소인지 확실하지 않지만, onResume, onActivityCreated, onResume, onCreateViewonAttach을 시도했습니다. 나는 파편이 나중에 코드에 실제로 보이지 않는다고 생각하고 있지만 확실하지는 않습니다. 두 조각이 모두 표시되는시기를 결정하십시오.

내가 사용하려고 해요 코드입니다 : 조각은 사용자가 볼 때

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout 
    android:orientation="horizontal" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent" 
> 
    <fragment android:name="com.app.Fragment1" 
      android:id="@+id/fragment1" 
      android:layout_weight="1" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
    /> 

    <fragment android:name="com.app.Fragment2" 
      android:id="@+id/fragment2" 
      android:layout_width="0dp" 
      android:layout_weight="2" 
      android:layout_height="fill_parent" 
    /> 
</LinearLayout> 

public class Fragment2 extends SherlockListFragment 
{ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 

     setHasOptionsMenu(true); 

     final Fragment1 f1 = (Fragment1)getFragmentManager().findFragmentById(R.id.fragment1); 
     final Fragment2 f2 = (Fragment2)getFragmentManager().findFragmentById(R.id.fragment2); 

     Boolean isVisible = (f1 != null && f1.isVisible() && f2 != null && f2.isVisible()); 
    }  
} 

답변

0

onStart()입니다, 그래서 난 그냥 안전을 위해 onResumeisVisible 코드를 넣는 것이 좋습니다.

+0

'isVisible' 대신'isAdded'를 사용하여 작동하도록했습니다. 응답 해 주셔서 감사합니다. –

+0

아, 수명주기의 다른 방법으로 단편이 추가되었지만 작동하게되어 기쁩니다! – hwrdprkns

관련 문제