HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaCritical

Using context in a fragment

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
contextfragmentusing

Problem

How can I get the context in a fragment?

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);
}

Solution

You can use getActivity(), which returns the activity associated with a fragment.

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

Context

Stack Overflow Q#8215308, score: 1440

Revisions (0)

No revisions yet.