Difference between getApplicationContext() and this in Android - android

I know this is a basic question but i have seen that using the method getApplicationContext() to get the context work at places where the "this" keyword does't work, especially inside an onClickListener.
Why is this?

In the case of an OnClickListener, this is the anonymous class of the OnClickListener, therefore not a Context.
Whereas calling that method works because it's from the Activity class.
Alternatively, MyActivity.this works as well.

getActivity(): Used inside a Fragment to get the context of activity it is currently associated to.
this: Returns the context of current block in which it is called. If it is called inside an onClickListener then it would return the context of that listener, not the activity.
MyActivity.this: Returns the context of the activity. This can be used at the place of getActivity() as an alternate. (MyActivity should be read as the activity name you are using).

Related

Cant resolve constructor ArrayAdapter

Can't use ArrayAdapter in a custom dialog.
This is the error I am getting
You're getting that error because the ArrayAdapter constructor requires the first argument to be a Context object. Since you're calling the constructor from inside an OnClickListener, this refers to the listener, not your activity (i.e. not a Context).
You can qualify the this keyword with the name of your outer class. Assuming that code is written inside of MainActivity, you could write MainActivity.this instead.
Using this won't work since you are in the scope of the OnClickListener.
You should try getContext() or getApplicationContext() instead of this.

What context to use in Multi activity android app?

I'm badly confused and hope to get your help understanding this concept.
I have an app with 3 activities, splash, login and main and the main activity is a multi fragment drawer activity that uses sqlite.
In my fragments i need to use context many places. I used to have a static context in my main activity defined and passed that around but in another questions someone suggested not to use static context to avoid leaks and i took the advice.
I had to change a few things and got things working. I use getapplicationcontext() but now my application now consistently crashes. The crashes are more prominent when the app is put in background.
My question is, which activity's context should i be using to start with? Splash? Login or main? How would you get access to the context in resume?
Thanks
If you are in A fragment you can use getActivity() to access its container activity context.
If you are try to access activity context from an adapter class or dialog, you must pass the activity context to the constructor of the adapter class or dialog
If you are in an activity you can use this or yourActivity.this as per the situation.
For example
1)If you want to access activity context from onResume() or onCreate() you can simply use this.
2)If you are try to access activity context from an inner class like retrofit call then you must use yourActivity.this for activiy context

Is there any difference between Activityname.this() & this?

Is there any difference between Activityname.this() & this in Android?
I am trying to open an activity from same activity with button in dialog box? I am using getApplicationContext() in intent. In some mobiles it works, but in others it force closes?
Between ActivityName.this and this which one I should use & why?
Is there any difference between Activityname.this() & this in Android ?
This depends on where you are calling it from. If you are inside the Activity, not inside of a listener or inner class like in onCreate then no. They both refer to the Activity context.
If you are say inside of an onClickListener then yes. this refers to the listener and you need to use ActivityName.this or something like
someButton.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
Intent i = (v.getContext(), NextActivity.class); use the button context which will be the same as the activity context
startActivity(i);
}
});
This will be the same as when using a Dialog or AlertDialog, you will want to use ActivityName.this
This is an answer that talks about the difference of Contexts but there's a better one I will see if I can find
A great Context explanation
Edit for more completeness
AFAIK, getApplicationContext() or ActivityName.this is fine for Toasts. The example in the docs uses getApplicationContext(). But the Toast Docs says
Parameters
context The context to use. Usually your Application or Activity object.
So there may be certain instances where one is better but I have always used Activity Context and I guess I will until I am corrected on this.
no MyActivity.this is the same thing as just using this when you are in the activity itself and not something like a runnable where this would refer to the runnable and not the context
you should always use this or getActivity() if in a fragment and never use getApplicationContext()
check here for why you shouldn't use getApplicationContext()
getApplication() vs. getApplicationContext()

Android. Access dialog's parent variables

From my activity I call showDialog(0) and then in the switch of the method onCreateDialog I would like to access a variable from the parent activity. It doesn't seem to work though. I've tried
this.neededVariable
or
((MyActivity)dialog.getOwnerActivity()).neededVariable
but they don't seem to work. Any ideas? It might be something simple which I'm not getting cause I'm very tired :)
you can pass the value of that variable to your dialog in setArgumenet, or you can use Fragment Callback to communicate the Activity, here is the example of callback
When you are in dialog you should use dialog context to interact with your dialog and when you want your Activity context you can use your activity name like :
YourActivityName.this
here is a sample that i've used in dialog code to access a TextView inside of the parent activity :
((TextView)DayReportActivity.this.findViewById(R.id.edtDailyCPCode)).setText(CPCodeText);

Android context from tab activity

I have a helper class that I need context so I can access the SharedPrefences. Other posts recommend passing in the application context on instantiation of the helper class. So I made that change, it works very well except within a tab activity. The tab activity need to call a webservice to determine what data to display. The helper class makes the webservice call.
You can call getContext() from any activity. If the helper class is defined as a subclass of the activity, it can call it directly. Otherwise, passing the context through instantiation would be my second choice. I agree, it's not pretty passing contexts everywhere. There are probably some complicated OOP patterns you could use to avoid this, but I can't see it being an advantage overall.
If you get a null pointer you might be calling the function too early. In what function are you calling it?

Categories

Resources