androidandroid-fragmentsandroid-viewpagerandroid-tablayouttabactivity

Use of nullable in onCreateView and onCreatedView


I have two fragments classes say A and B and one mainactivity class.I looked some tutorials and have added recycler view(this is not the main concern though).Following up the tutorial on my both fragment classes i have this code

@Nullable
@Override
public View onCreateView (LayoutInflater inflater, @Nullable 
    ViewGroup container, @Nullable Bundle savedInstanceState){
    View rootView = inflater.inflate(R.layout.absent, container, false);
    return rootView;  
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);  
}

so what is the use of @Nullablehere?.i have searched this many times but i can't understand it's use and even that tutorial haven't mentioned anything about @nullabe.Can anyone make me understand about this? please don't bother in talking about recycler view because i have already added it.


Solution

  • The @Nullable annotation indicates a variable, parameter, or return value that can be null You ever get a "NullPointerException"? That's because the value is null.

    Integer x; 
    

    'x' Will be null because you haven't assigned a value to it so there will be errors using it. But what if you have a variable that might actually be null in some use cases? Well, you'd use the @Nullable annotation meaning that it's okay to be null.

    @Nullable Bundle savedInstanceState
    

    Bundle in this case might have nothing stored in it. So it's okay to be null when the method is called.

    Same goes for the method onCreateView() meaning that the return can be null