2013-07-17 32 views
1

我在我的onCreate()方法中以編程方式添加了SupportMapFragment,其中包含getChildFragmentManager()Android:谷歌地圖API v2如何清理子女SupportMapFragment

當我在活動結束後重新打開應用程序時,該應用程序似乎正在渲染沒有標記的舊小孩SupportMapFragment。舊的孩子片段也不可互動。

如何解決這個生命週期問題SupportMapFragment?我是否需要調用特定的分離方法或其他方法?

回答

1

問題的關鍵是如何處理我的孩子的碎片。

每當父片段調用onCreate時,子片段將被重新創建。

我做了如下處理我的孩子片段,但也有可能是一個更好的辦法:

private static final String TAG_FRAGMENT_MAP = "TagFragmentMap"; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    // ... 
    if (savedInstanceState == null) { 
     // create the fragments for the first time 
     ft.add(R.id.view_flip, new SupportMapFragment(), TAG_FRAGMENT_MAP); 
     ft.commit(); 
    } 
} 

// ... 

public void onViewStateRestored(Bundle savedInstanceState) { 
    super.onViewStateRestored(savedInstanceState); 
    mMapFragment = (SupportMapFragment)findFragmentByTag(TAG_FRAGMENT_MAP); 
}