0

If I have an onClickListener that is called when user clicks on a button and that listener does a callback to a method of the containing fragment which does getContext().
Is there a case that the getContext() in this sequence could return null?
I am trying to understanding if doing Context c = getContext(); if(c == null) return; makes sense in this specific case/flow or is a useless defensive check

Update for the referred links in the comments:
I understand that when we access the context via a callback it can be null e.g. when handling a network request.
But a callback that is called when the user did an interaction with a UI element as in the case I describe is there a situation that can be null.
Flow is:

  1. User clicks a button
  2. Action listener is called and calls another callback
  3. The callback listener inside the containing fragment does getContext()

Can this flow ever end up with getContext() returning null?

Jim
  • 3,845
  • 3
  • 22
  • 47

0 Answers0