2014-03-01 28 views
0

最近我在閱讀完所有片段後開始使用newInstance()方法,這是正確的方式。但是,我不高興,它不按我期望的方式工作。我的問題是在我的newInstance()方法中調用setArguments()後,在onCreate(Bundle savedInstanceState)爲空。當FragmentPagerAdapter重新創建時,Fragment中的getArguments()爲空

我的片段:

public static CreateCoverFragment newInstance(CreateCoverModel createCoverModel) { 
    CreateCoverFragment createCoverFragment = new CreateCoverFragment(); 

    Bundle args = new Bundle(); 
    args.putParcelable(NvpKeys.CREATE_COVER_MODEL, createCoverModel); 
    createCoverFragment.setArguments(args); 

    return createCoverFragment; 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    Log.d(TAG, "onCreate()"); 
    super.onCreate(savedInstanceState); 

    Bundle args = getArguments(); 
    if (args != null) { 
     mModel = args.getParcelable(NvpKeys.CREATE_COVER_MODEL); 
    } else { 
     Log.e(TAG, "getArguments() is null...WTF!"); 
    } 
    } 

在我FragmentPagerAdapter,我回:

CreateCoverFragment.newInstance(mCreateItineraryModel.getCreateCoverModel());

和我的日誌:

03-01 16:15:50.204 30043-30043/com.blunka.harry D/com.blunka.harry.itinerary.BaseItineraryPagerAdapter﹕ getItem(position: 0) 
03-01 16:15:50.204 30043-30043/com.blunka.harry D/com.blunka.harry.itinerary.create.CreateCoverFragment﹕ onCreate() 

添加新頁面,並呼籲後notifyDataSetChanged()PagerAdapterCreateCoverFragment被重新創建,這裏是問題所在。

03-01 16:15:54.934 30043-30043/com.blunka.harry D/com.blunka.harry.itinerary.BaseItineraryPagerAdapter﹕ getItem(position: 1) 
03-01 16:15:54.934 30043-30043/com.blunka.harry D/com.blunka.harry.itinerary.create.CreateCoverFragment﹕ onCreate() 
03-01 16:15:54.934 30043-30043/com.blunka.harry E/com.blunka.harry.itinerary.create.CreateCoverFragment﹕ getArguments() is null...WTF! 

編輯:

我加了一些記錄到CreateCoverFragment的生命週期方法和我更糊塗了。

9:23:31.552 4129-4129/com.blunka.harry D/com.blunka.harry.itinerary.BaseItineraryPagerAdapter﹕ getItem(position: 0) 
03-01 19:23:31.552 4129-4129/com.blunka.harry D/com.blunka.harry.itinerary.create.CreateCoverFragment﹕ newInstance() 
03-01 19:23:31.562 4129-4129/com.blunka.harry D/com.blunka.harry.itinerary.create.CreateCoverFragment﹕ CreateCoverFragment 
03-01 19:23:31.562 4129-4129/com.blunka.harry D/com.blunka.harry.itinerary.create.CreateCoverFragment﹕ onAttach() 
03-01 19:23:31.562 4129-4129/com.blunka.harry D/com.blunka.harry.itinerary.create.CreateCoverFragment﹕ onCreate() 

添加頁面到PagerAdapter ...

03-01 19:23:32.402 4129-4129/com.blunka.harry D/com.blunka.harry.itinerary.BaseItineraryPagerAdapter﹕ getItem(position: 1) 
03-01 19:23:32.402 4129-4129/com.blunka.harry D/com.blunka.harry.itinerary.create.CreateCoverFragment﹕ CreateCoverFragment 
03-01 19:23:32.402 4129-4129/com.blunka.harry D/com.blunka.harry.itinerary.create.CreateCoverFragment﹕ onAttach() 
03-01 19:23:32.402 4129-4129/com.blunka.harry D/com.blunka.harry.itinerary.create.CreateCoverFragment﹕ onCreate() 
03-01 19:23:32.402 4129-4129/com.blunka.harry E/com.blunka.harry.itinerary.create.CreateCoverFragment﹕ getArguments() is null...WTF! 

如何以及爲什麼是默認的構造函數,onAttach(),和的onCreate()方法得到被稱爲第二次沒有的newInstance()的onDestroy ()或onDetach()首先被調用?

編輯:

我是個傻瓜。我在不同的新片段中使用了CreateCoverFragment的默認構造函數。

回答

1

的getArguments()獲取任何論據,當片段首先實例化一個包中提供的 - 在你的情況下,當以下行設置:

CreateCoverFragment createCoverFragment = new CreateCoverFragment(); // No bundle set at this point 

根據你的代碼,當的onCreate被調用,片段沒有提供包。把你提取bundle參數的代碼放到onCreateView()方法中,它應該可以正常工作。

+0

如果我在onCreateView()中檢查它,getArguments()仍然爲空。另外,從我看到的,通常的做法是提取onCreate()中的參數。 – clocksmith

+1

例如,http://developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.html – clocksmith

+0

我明白你的意思,但如果你仔細觀察他們的風格,你會看到他們的碎片是靜態的內部他們的主要活動的類別。根據我的經驗(我遵循commonsware參考書),一旦調用包含該包的片段的類加載了片段,就會提取參數 - 因此它們位於onCreateView()內(因爲這是創建佈局的位置)。你可以發佈調用這個片段的活動的代碼,我可以嘗試在我的系統上運行它? – ucsunil

相關問題