2012-01-04 8 views
12

조각은 우스운 일이지만, 일단 자신의 단점을 알게되면 여러 장치에서 좋은 코드를 작성하는 데 매우 유용합니다.조각/활동주기 및 오리엔테이션 변경

그러나 오리엔테이션 변경 버그를 수정하는 동안 벽에 부딪 혔습니다. 내 프래그먼트가 작동하려면 Activity에 포함 된 View에 대한 액세스가 필요합니다. Activity가 나를 포함하여 Activity & 프래그먼트 수명주기가 상호 작용하는 방식을 찾으려고 노력하고 있습니다. 이 활동 -> 조각 라이프 사이클에 선도

// Only add a fragment once, as after it's been added it cannot be replaced (Even though there is a .replace() method. Which is a massive gaping hole in fragments as a technology if you ask me) 
if(savedInstanceState == null) { 
    MainMenuFragment menu= new MainMenuFragment(); 
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 
    transaction.replace(R.id.menuFrame, menu); 
    transaction.commit(); 
} 

:이 onCreate() 방법이다 내가 내 활동에 조각을 추가 해요

보기

01-04 15:17:27.226: W/SinglePaneActivity 0: onCreate() 
01-04 15:17:27.378: W/MainMenuFragment 0: onAttach() to SinglePaneActivity 0 
01-04 15:17:27.378: W/MainMenuFragment 0: onCreate() 
01-04 15:17:27.453: W/MainMenuFragment 0: onActivityCreated() 
01-04 15:17:27.476: W/MainMenuFragment 0: onStart() 
01-04 15:17:27.476: W/SinglePaneActivity 0: onStart() 
01-04 15:17:27.476: W/SinglePaneActivity 0: onResume() 
01-04 15:17:27.476: W/MainMenuFragment 0: onResume() 

오리엔테이션 변화는 그러나이 잘못되고 있음을 강조 대개의 경우, 단편 onCreate()은 부모 이후에 호출되지 않습니다. 활동 onCreate(). 활동도 생성되기 전에 가리키고하는 조각의 onAttach()의 첫 번째 라이프 사이클 호출 (null는 인수로 전달됩니다) 발생

01-04 15:10:49.589: W/MainMenuFragment 0: onPause() 
01-04 15:10:49.589: W/SinglePaneActivity 0: onPause() 
01-04 15:10:49.589: W/MainMenuFragment 0: onStop() 
01-04 15:10:49.589: W/SinglePaneActivity 0: onStop() 
01-04 15:10:49.589: W/MainMenuFragment 0: onDestroyView() 
01-04 15:10:49.589: W/MainMenuFragment 0: onDestroy() 
01-04 15:10:49.589: W/MainMenuFragment 0: onDetach() 
01-04 15:10:49.609: W/SinglePaneActivity 0: onDestroy() 
01-04 15:10:49.617: W/MainMenuFragment 1: onAttach() to null 
01-04 15:10:49.617: W/MainMenuFragment 1: onCreate() 
01-04 15:10:49.617: W/SinglePaneActivity 1: onCreate() 
01-04 15:10:49.890: W/MainMenuFragment 1: onActivityCreated() 
01-04 15:10:49.917: W/MainMenuFragment 1: onStart() 
01-04 15:10:49.917: W/SinglePaneActivity 1: onStart() 
01-04 15:10:49.921: W/SinglePaneActivity 1: onResume() 
01-04 15:10:49.921: W/MainMenuFragment 1: onResume() 

난이 발생하는 이유를 전혀 생각이 없다. 누구든지 액티비티가 생성되기 전에 Fragment.onAttach()이 호출되는 이유를 밝힐 수 있습니까?

내가 가지고있는 조각에는 포함 된 활동에 대한 액세스가 필요하지 않습니다 (UI 상호 작용 전까지) 예상대로 작동합니다. 도대체 내가 아무 생각이없는 onAttach() 방법이 왜

답변

11

아아,

01-04 15:46:23.175: W/MainMenuFragment 0: onAttach() to SinglePaneActivity 0 
01-04 15:46:23.179: W/MainMenuFragment 0: onCreate() 
01-04 15:46:23.246: W/MainMenuFragment 0: onActivityCreated() with Activity SinglePaneActivity 0 
01-04 15:46:23.269: W/MainMenuFragment 0: onStart() 
01-04 15:46:23.269: W/SinglePaneActivity 0: onStart() 

. 특히 활동이 있기 전에 "연결"이 발생하기 때문에 특히 그렇습니다.

내가 필요로하는 방법은 물론 onActivityCreated()이며, 이는 "창조"세트 Fragment lifecycle events의 최종 호출에서 발생합니다.

+0

제발 도와주세요 http://stackoverflow.com/questions/12331787/save-fragment-objects – user4o01