0

Inside onCreateView i can instance View with the inflate, in this way:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        // Inflate the layout for this fragment
        if(savedInstanceState == null) {
            v = inflater.inflate(R.layout.fragment_my_team, container, false);
            setUpRecyclerView(v);
        }

        return v;
    }

Now, if launch a second activity when return in the first activity, in this fragment, the View is null because onCreateView it's already called. I don't know a to instance the view.

Is there a solution of that?

mmarcoso
  • 1
  • 2
  • apart from the answer already given,I would suggest you instantiate your views in `onViewCreated()` method instead of `onCreateView()` – Aman Grover May 09 '20 at 18:43
  • Can i write the inflate inside onViewCreated() rather than onCreateView()? Like this: v = inflater.inflate(R.layout.fragment_my_team, container, false); But in onViewCreated() i didn't have the ViewGroup container, how can find it? – mmarcoso May 10 '20 at 07:32
  • No, onCreateView() is intended to inflate your views, as the name suggests, but if you're worried how to get the view reference that was inflated in onCreateView (), then onViewCreated() also has a view parameter that you can use to do findViewById() on your layout views. – Aman Grover May 10 '20 at 14:58
  • @AmanGrover can you explain with the code please? – mmarcoso May 11 '20 at 13:58

1 Answers1

0

Get rid of the if(savedInstanceState == null) validation and create & return your view every time onCreateView is invoked.

fraggjkee
  • 3,524
  • 2
  • 32
  • 38
  • The problem is that onCreateView is not invoked second time and the View is not instanceded. Outside onCreateView is there a way to instanced ViewGroup container? How? – mmarcoso May 10 '20 at 07:19
  • Fragment will not call onCreateView if it does not need to do so, i.e. if it already has a view provided by onCreateView. Use `getView()` if you need to access the previously created View. – fraggjkee May 12 '20 at 19:05