Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of Context to start another Activity

To start an Activity you need an Intent, like:

Intent i = new Intent(context, class)

So to fill in the context parameter, a couple of options are available:

  • Use MyActivity.this or just this
  • Use getApplicationContext()
  • Use getBaseContext()

And I'm sure there are one or two more options. These options all appear in some sort of tutorial, one uses the first, the next uses the third option.

So which one should I use? Does it even matter? Is it different for different cases?

like image 434
nhaarman Avatar asked Feb 10 '12 12:02

nhaarman


People also ask

Which method is used to start another activity?

To start an activity, call startActivity() and pass it your Intent . The system receives this call and starts an instance of the Activity specified by the Intent .

Can I start activity with application context?

Yup, simply use the context and call the startActivity() method from that context.

How do I Intent to start another activity?

To start an activity, use the method startActivity(intent) . This method is defined on the Context object which Activity extends. The following code demonstrates how you can start another activity via an intent. # Start the activity connect to the # specified class Intent i = new Intent(this, ActivityTwo.

What is the context of the activity?

In android, Context is the main important concept and the wrong usage of it leads to memory leakage. Activity refers to an individual screen and Application refers to the whole app and both extend the Context class.

Can application context be passed using Intent to another activity?

There is no way to pass the context to the target activity using Intent. However, you can pass context, integers, booleans, strings, instances and so on using a static method as shown above.

How do I start an activity from another application?

If both application have the same signature (meaning that both APPS are yours and signed with the same key), you can call your other app activity as follows: Intent LaunchIntent = getActivity(). getPackageManager(). getLaunchIntentForPackage(CALC_PACKAGE_NAME); startActivity(LaunchIntent);


2 Answers

Yes its different for different cases,

It depends on the scope. Suppose if you are creating a method in a global class that extends Application to create a Toast that is used in every class of your Application you can use getApplicationContext() to create it.

If you want to create a view that is restricted to that particular Activity you can use Activity.this

Also if you want to create an AlertDialog in some inner class say AsyncTask, then you have to use Activity.this, because the AlertDialog is to be linked to Activity itself.

Also don't use getBaseContext() just use the Context that you are having. For getting further information for the same you can see this Answer.

So, the answer to the real question is better to use Activity.this to start a new Activity.

Intent intent = new Intent(Current_Activity.this, Calling.class);
startActivity(intent);
like image 145
Lalit Poptani Avatar answered Oct 18 '22 19:10

Lalit Poptani


They are different for sure. These are different contexts, and should be used with the least possible scope(context).

For example if we can use Activity's Context instead of ApplicationContext, one should use the activity context, same applies to application context, and base context.

like image 34
jeet Avatar answered Oct 18 '22 18:10

jeet