What exactly is Context doing on Android?

Asked 2 years ago, Updated 2 years ago, 142 views

When exactly is the Context class used on Android? I went to the developer site and read it, but I don't understand it.

android android-context

2022-09-21 21:45

1 Answers

Simply put, it's a class about the current state of an app or object. GetApplicationContext(), getContext(), getBaseContext(), or You can get context with this in the activity

Usually when you create a new object, such as View, adapter, or listener,

    TextView tv = new TextView(getContext());
    ListAdapter adapter = new SimpleCursorAdapter(getApplicationContext(), ...);

I write it like this.

When accessing Standard Common Resources such as LAYOUT_INFLATER_SERVICE and Shared Preferences

    context.getSystemService(LAYOUT_INFLATER_SERVICE)   
    getApplicationContext().getSharedPreferences(*name*, *mode*);        

I write it like this.

Also, content providers, broadcasts, intents

    getApplicationContext().getContentResolver().query(uri, ...);

I write it like this


2022-09-21 21:45

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.