Update fragment data after newInstance()

2019-07-29 07:24发布

Basically I have my fragment

        public class FragmentDashboard extends Fragment {

           public static FragmentDashboard newInstance() {
                FragmentDashboard frag = new FragmentDashboard();
                return frag;
            }

            public void updateData(Object object){
               myTextView.setText(object.getField);
               //update views with object values
            }
           @Override
           public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

            View view = inflater.inflate(R.layout.fragment_dashboard, container, false);

            myTextView = (TextView) view.findViewById(R.id.myTextView );
           }
}

Then in my activity I need to update data I do:

FragmentDashboard fragmentDashboard = (FragmentDashboard) getSupportFragmentManager().findFragmentById(R.id.layFragment);
fragmentDashboard.updateData(myObject)

This works good if I am making the call on my activity after the Fragment has been displayed (like from an asynctask on completed).

The problem I am running into is having to run the updateData right after I add the fragment on my activity's onCreate().

final FragmentTransaction ft = fragmentManager.beginTransaction();

 FragmentDashboard fragmentDashboard = FragmentDashboard.newInstance();
 ft.replace(R.id.layFragment, fragmentDashboard);
 ft.commit();

 fragmentDashboard.updateData(myObject) 

Running this I get a NPE because the fragment's onCreateView hasn't been run yet and the myTextView is not initialized

I don't want to add parameters on newInstance as I'd like to avoid making the object as parcelable. Any ideas ?

2条回答
我只想做你的唯一
2楼-- · 2019-07-29 08:06

You can use a local field to contains your data and use it in your onCreateView method :

public class FragmentDashboard extends Fragment {

    private Object myData=null;
    private TextView myTextView = null;

    public static FragmentDashboard newInstance() {
        FragmentDashboard frag = new FragmentDashboard();
        return frag;
    }

   public void updateData(Object object){
       myData = object;
       if(myTextView != null)
           myTextView.setText(myData);
   }

   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       View view = inflater.inflate(R.layout.fragment_dashboard, container, false);
       myTextView = (TextView) view.findViewById(R.id.myTextView );
       if(myData != null && myTextView != null)
           myTextView.setText(myData);
   }
}
查看更多
Explosion°爆炸
3楼-- · 2019-07-29 08:15

It isn't a good practice to set Data like updateData(Object ) . Make your model class parcelable or serializable and pass it in putExtra and get it in onViewCreated.

    public static FragmentDashboard newInstance(Object object) {
                Bundle args = new Bundle();
                args.putParcelable("yourModelClass",object);
                FragmentDashboard frag = new FragmentDashboard();
                frag.setArguments(args);
                return frag;
     }    

And in onViewCreated

if(getArguments != null)
    yourModelClassObject = getArguments().getParcelable("yourModelClass");

 if(yourModelClassObject != null)
     textView.setText(yourModelClassObject.getField());

I have written code orally . May contain mistakes.

查看更多
登录 后发表回答