0

In Android programming, How many context we can use at a time in different simultaneously executed Threads Or Asynchronous tasks ?

Different invoking methods by which you can get context

  1. getApplicationContext(),
  2. getContext(),
  3. getBaseContext()
  4. or this (when in the activity class).

Also check this :

When to call activity context OR application context?

If I am running many Threads at a same time so is it Ok to pass getApplicationContext() in all of them or Some issue will occurs ???

jakir hussain
  • 296
  • 1
  • 18
  • 2
    Possible duplicate of [Difference between getContext() , getApplicationContext() , getBaseContext() and "this"](https://stackoverflow.com/questions/10641144/difference-between-getcontext-getapplicationcontext-getbasecontext-and) – Pavneet_Singh Dec 12 '17 at 05:17
  • I m asking is there any limit of using these methods at same time ??? – jakir hussain Dec 12 '17 at 05:20
  • 1
    what is your problem actually? you dont know which one to use in your particular use case? – pskink Dec 12 '17 at 05:25
  • If i am running many threads at a same time so is it ok to pass getApplicationContext() in all of them – jakir hussain Dec 12 '17 at 05:30
  • see [this](https://stackoverflow.com/questions/1026973/whats-the-difference-between-the-various-methods-to-get-a-context) or [this](https://blog.mindorks.com/understanding-context-in-android-application-330913e32514) – pskink Dec 12 '17 at 05:31

3 Answers3

1

Yes, you can pass getApplicationContext() any number of time (Background Tasks ) you want, getApplicationContext() simply returns context of the application.

0

you can find many of answers about difference between Context uses, Link

every context method have some limit to use it.

If you want to use a only One method or only one way to initialise some method calling in entire Activity.

Then declare Context as Globally as below, it have no limit to use in Activity.

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    private Activity thisActivity;  //Activity object instance

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        thisActivity = this;    // Initialise with this(Context)

           //Or thisActivity = MainActivity.this;
    }
}

use thisActivity in place of Context

ND1010_
  • 2,848
  • 16
  • 33
0

Yes, you can pass getApplicationContext() any number of time you want, getApplicationContext() simply returns context of the application.

jakir hussain
  • 296
  • 1
  • 18
Rahul Samaddar
  • 189
  • 1
  • 8