Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
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).

Example:

public class Animal extends Fragment { 

  Context thiscontext;

  @Override

  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
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94.1k users

Browse Categories

...