Android fragment can't read bundle. Bundle is always NULL

僤鯓⒐⒋嵵緔 提交于 2020-06-17 05:06:56

问题


I wrote a custom static method to create my fragment. Fragment is a subclass of android.support.v4.app.Fragment class.

Method to create my fragment is bellow.

public static AddItemFragment newInstance(UUID listId, UUID itemId){

        AddItemFragment fragment=new AddItemFragment();
        Bundle bundle=new Bundle();
        bundle.putSerializable(EXTRA_DATA_LIST_ID,listId);
        bundle.putSerializable(EXTRA_DATA_ITEM_ID, itemId);
        fragment.setArguments(bundle);
        return fragment;
 }

In my onCreate method, I am attempting to read data from bundle.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mListId = (getArguments().getSerializable(EXTRA_DATA_LIST_ID) == null ? null : (UUID) getArguments().getSerializable(EXTRA_DATA_LIST_ID));
    mItemId = (getArguments().getSerializable(EXTRA_DATA_ITEM_ID) == null ? null : (UUID) getArguments().getSerializable(EXTRA_DATA_ITEM_ID));
}    

Well the problem is that getArguments() method never returns bundle. It always returns NULL. I don't understand why. savedInstanceState is NULL as well.


回答1:


Silly me was overriding bundle set in the Fragment with Activity's savedInstanceState bundle which at that point is NULL.

DUH...



来源:https://stackoverflow.com/questions/29596660/android-fragment-cant-read-bundle-bundle-is-always-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!