Get the Application Context In Fragment In Android?

AndroidAndroid FragmentsFragment

Android Problem Overview


I have stored some data to a Global Class By using the Application Context In One Activity. Later I have to Retrieve those values in A Fragment. I have done something like this to store in Global Class.

AndroidGlobalClass  AGC = ((AndroidGlobalClass) getApplicationContext());
AGC.setUser_access("XYZ");
AGC.setFirst_name("ABC");

And In the Manifest I have done :

<application
    android:name=".AndroidGlobalClass"
    android:theme="@style/AppTheme" >
    <activity
       android:name="abc.SignInActivity"
       android:label="@string/app_name" >
       <intent-filter>
          <action android:name="android.intent.action.MAIN" />
          <category android:name="android.intent.category.LAUNCHER" />
       </intent-filter>
    </activity>
</application>

Now When I am Trying to Get the Application Context Using this... I am not getting the Context...

AndroidGlobalClass  AGC = ((AndroidGlobalClass) getApplicationContext());

This is My Fragment Activity

public class Fragment_NewsFeed extends Fragment {
    public Fragment_NewsFeed() {
    }

    RestImplimentationMethods RIM;
    AndroidGlobalClass AGC;
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_newsfeed, container, false);
        return rootView;
    }
}

Android Solutions


Solution 1 - Android

You can get the context using getActivity().getApplicationContext();

Solution 2 - Android

Use

> getActivity().getApplicationContext()

to obtain the context in any fragment

Solution 3 - Android

In Kotlin we can get application context in fragment using this

requireActivity().application

Solution 4 - Android

you can define a global variable :

private Context globalContext = null;

and in the onCreate method, initialize it :

globalContext = this.getActivity();

And by that you can use the "globalContext" variable in all your fragment functions/methods.

Good luck.

Solution 5 - Android

Try to use getActivity(); This will solve your problem.

Solution 6 - Android

Pretty late response but you can do the following in Kotlin:

activity?.applicationContext?.let { SymptomsAdapters(it, param2, param3, ...) }

(?.) is for safe null operation to prevent from the null pointer exception.

You do not want to create a new context as that can lead to memory leaks when interchanging between fragments or when the device changes rotation.

And as someone mentioned above, in Java you can obtain context in a fragment by doing the following:

getActivity().getApplicationContext()

Solution 7 - Android

In Support Library 27.1.0 and later, Google has introduced new methods requireContext() and requireActivity() methods.

Eg:ContextCompat.getColor(requireContext(), R.color.soft_gray)

More info here

Solution 8 - Android

It's working for me

private Context contextUApp;

... OnCreate...

contextUApp = view.getContext();

or use

requireContext()

Solution 9 - Android

Add this to onCreate

// Getting application context
        Context context = getActivity();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNRahmanView Question on Stackoverflow
Solution 1 - AndroidSalGadView Answer on Stackoverflow
Solution 2 - AndroidAakashView Answer on Stackoverflow
Solution 3 - AndroidEldhopjView Answer on Stackoverflow
Solution 4 - AndroidNabzView Answer on Stackoverflow
Solution 5 - AndroidAshwin S AshokView Answer on Stackoverflow
Solution 6 - AndroidQasim WaniView Answer on Stackoverflow
Solution 7 - AndroidJithin JudeView Answer on Stackoverflow
Solution 8 - AndroidVictor Sam VSView Answer on Stackoverflow
Solution 9 - AndroidThiagoView Answer on Stackoverflow