1
我在旋轉屏幕時編寫了以下代碼片段重新生成。刷新片段在重新加載或在旋轉後
片段setArguments是一個「java.lang.IllegalStateException」錯誤發生。
還有什麼更好的方法呢?
的參考:retain the fragment object while rotating
的參考:refresh fragment at reload
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String tag = "my_fragment";
FragmentManager fragmentManager = getFragmentManager();
Fragment f = fragmentManager.findFragmentByTag(tag)
if(f == null) { //or if(null == savedInstanceState){
//create Fragment
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
Fragment homeFragment = new Home();
fragmentTransaction.add(R.id.mainFragement, homeFragment, tag);
fragmentTransaction.commit();
} else {
//renew Fragment
Bundle newArgs = //want to renew Args
f.setArguments(newArgs);
ft.detach(f);
ft.attach(f);
ft.commit();
}
}
01-24 16:00:54.905: E/VideoPlayerActivity.java # onNewIntent:319(22727): Fragment already active
01-24 16:00:54.905: E/VideoPlayerActivity.java # onNewIntent:319(22727): java.lang.IllegalStateException: Fragment already active
01-24 16:00:54.905: E/VideoPlayerActivity.java # onNewIntent:319(22727): at android.support.v4.app.Fragment.setArguments(Fragment.java:551)