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()
Related
On button click I'm starting a new activity like this:
Intent(requireContext(), SecondActivity::class.java).apply {
putExtra(RECORDED_SUCCESSFULLY, filePath)
(activity as FirstActivity).finish()
startActivity(this)
}
But because I need to finish this activity before creating a new one, requireContext() here will be occasionally equal null and this causes a crash. So my question is, can I use getApplicationContext() or should I use safe call with getContext() in this situation?
It's prefered to use Activityname.this or getApplicationContext().
Remember there are two types of contexts in android and when you are dealing with activity components such as finish use Activityname.this
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
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).
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);
I have an asynch task with my app which goes to a website, grabs the results from the API and appends a number of clickable textviews to an existing LinearLayout.
However I want to be able to launch a new activity when the textview is clicked. This isn't possible with the asynch class defined in a seperate file, would it be easier to define it as an inline class within the activity?
You can always pass Context to your async class.
A better approach would be to have callbacks (listeners) in the calling class for the async to call back to.
One approach is to inflate your TextViews from an XML file that declares an onClick attribute, naming a method defined in your Activity.
Do not use a context as an Activity! You will probably receive a cast error anyway. Instead, you can pass the activity as a function parameter, like this:
public void function(Activity act)
{
Intent intent = new Intent(act, newActivity.class);
act.startActivity(intent);
}
Or overload the constructor to accept the activity as a parameter. But I strongly suggest you to check you code. If you are calling an activity, you, probably, should be within another one, don't you agree? But, I Know that sometimes we have to make a few concessions, in order to make things work properly. So, use it wisely.