Error non-default constructors in fragments

后端 未结 5 2148
长发绾君心
长发绾君心 2020-11-28 14:35

I am dealing with maps apiv2. And I am getting the following error while coding for Dialog Fragment class.

Error : Avoid non-de

5条回答
  •  一整个雨季
    2020-11-28 15:28

    Because of the nature of fragment and how there managed and shown on your screen it's very recommended not to create a non default constructor and in many cases this would cause problems in run time. In case you should do something like this:

    public static final GridFragment newInstance(String tabId)
    {
        GridFragment f = new GridFragment();
        Bundle bdl = new Bundle(2);
        bdl.putString(TAB_ID, tabId);
        f.setArguments(bdl);
        return f;
    }
    

    and in onCreate extract the needed data from the bundle:

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        String tabId = getArguments().getString(TAB_ID);
        }
    

    And take a look at this question as well:

    Do fragments really need an empty constructor?

提交回复
热议问题