Explore Courses Blog Tutorials Interview Questions
0 votes
in Java by (3.5k points)

I need to use my database whose constructor takes in the context, but getApplicationContext() and FragmentClass.this don't work so what can I do?

Database constructor

public Database(Context ctx)


    this.context = ctx;

    DBHelper = new DatabaseHelper(context);


1 Answer

0 votes
by (46k points)

To get the context in a fragmented use getActivity(), which renders the activity associated with a fragment.

The activity is a context (since Activity continues Context).


public class Animal extends Fragment { 

  Context thiscontext;


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


    thiscontext = container.getContext();

Click here to read more about getActivity().

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories