Why is context passed to the constructor of Intent? - java

I am trying to understand why we pass a Context instance to the Intent constructor? Why isn't it enough to do new Intent(SomeActivity.class)? Does Android enforce some restrictions or what?
I was trying to look at the code but all I find is that it gets the package name.
Intent documentation

To identify an Activity in an Android application unambiguously, you need to have both, the name of the application (aka Android application package), and the full name of the activity (java package name + class name of activity class). These are exactly those two parameters you give in constructor. Context is used to get Android application package name, and the class to get full class name.
An Activity with the same full name can be used in two applications. If you do not provide Context, then Android won't know which application an activity belongs to.

As you guessed, the Context in the constructor is used to get the package name of the application.
Inferring the package name from the class as in SomeActivity.class.getPackage().getName() does not work in every case, as it could be different from the application's package name.

Related

Android 25 Java : Using methods that require Context in a different Class - Require assistance

I created a simple little directory listing method in the default MainActivity.class
I was able to get it to function the way I wanted it to, however, when I moved the method to a different class and called it in MainActivity, I wound up getting a lot of Context Errors all over the place. After searching the web I am stumped and require assistance.
The code where it errors out is for the Context for FileArray:
(public class Utilities extends Activity)
arrayAdapter = new FileArrayAdapter(getApplicationContext(),R.layout.custom_explorer,dir);
Method Name:
public void listDirectories(ListView listView, File directory)
It errors out on the getApplicationContext, this method worked just fine in MainActivity.
I have not implemented Fragments to MainActivity yet, wanted to get the working functionality first then spread out for OOP; then call by Fragment.
Hope someone can help, any further info I am happy to share.
Thank you for the timely response: ρяσѕρєя K & Nilabja.
Ya I guess it just needed another person to suggest feeding Context as a param. I did that once but it did not work so I passed it off, attempted once again from Nilabja suggestion. Yip that was the solution.
utilities.ListDirectory(mainList, root, getApplicationContext());
public void listDIR(ListView listView, File directory, Context setContext)

SQLiteOpenHandler why do we need the Context variable?

I've started programing my first app in android, and noticed that in the constructor of SQLiteOpenHandler that is:
public SQLiteOpenHelper (Context context, String name, SQLiteDatabase.CursorFactory factory, int version)
I have a "Context" variable which role is unclear to me, because my intuition is to think that only one DB could exist with the same name NO mattar in what context I create it. I've looked in the manual and it said:
Parameters:
context to use to open or create the database
which did help me figure out the Context role ether.
Therefore, I'd like to ask
What is the role of the Context in the DB creation?
Will different Data Base instances will be created for different Contexts but same DB name, factory and version?
What is the role of the Context in the DB creation?
It is needed to obtain your app's package name when constructing the full package-private path for the database file.
Specifically, Context.getDatabasePath() called by openOrCreateDatabase(), called by SQLiteOpenHelper.
Will different Data Base instances will be created for different Contexts but same DB name, factory and version?
No, provided that the contexts are withing the same app i.e. share the same package name defined in the manifest.
If the apps are different, the app-private data paths will be different and the database files will be different.

Context having reference to class in android

Hi i am trying to understand the use of context though i couldn't. Following is a program using context. My question is what is the significance of " context = class.this " ?
class public VcardActivity extends Activity
{
String Vcard = "vcard";
Context context;
}
public void onCreate ( Bundle bn )
{
super.onCreate(bn);
setContentView(R.layout.main);
context = VcardActivity.this;
}
Your current code doesn't show the use of context. It shows that the Activity is a context.
TextView someText=new TextView(context);
This code of mine shows, I am passing a context into the constructor of a TextView in order to make this object. The reason is, this object needs to know the information, state of the current context, and this is the reason why many views, classes, helpers needs a context.
context = VcardActivity.this;
in your code you are having your activity object to assign to the Context context. This works because Activity class inherits from Context and many classes needs a Context to create it.
In your case, the field context is not necessary at all. It rather is used as a shortcut to VcardActivity.this here. You could remove it without any problems and use VcardActivity.this or even only this where you used to use context.
You don't need to create a separate Context variable inside of an Activity. You use Context for certain objects/methods that need to know what is starting them. Activity already has a Context so you don't need to create it. If you need to use Context within an Activity, say when creating an Intent you can just use ActivityName.this or here VcardActivity
See this SO answer for a good explanation of using which kind of Context when.
Context Docs

dealing with application context

I'm new to android and java.
I'm rearranging some of the classes in my app into separate class files. I had a onLocationListener class in my main activity class file. I moved the class to a separate java class file. Then, however the following code will not compile . . .
public void onProviderDisabled(String provider)
{
Toast.makeText( getApplicationContext(),
"Gps Disabled",
Toast.LENGTH_SHORT ).show();
}
The getApplicationContext won't compile when this code is in a separate file. I tried this. and mainactivityname. but nothing seems to work. So I suppose this problem can be formed into a the following question:
How do you state the application context from code that exists in separate java class files outside the main activity file?
thanks, Gary
getApplicationContext() is a method of class Context, so you can only call it from a class or object that in some way extends Context. You factored your code out of Activity, which is such a class. Your solution, then, is for the Context class that contains your new class or object to pass its context in so that your new class can use it.
Your code inside your main Activity would look something like this:
MyOwnClass ownObject = new MyOwnClass();
// you have to implement setApplicationContext
ownObject.setApplicationContext( this.getApplicationContext() );
It's probably a good idea to get the application context right away, since it'll be stable for the lifetime of your app, unlike the Activity context which could go away on something as simple as an orientation change.

Context in android

i am a newbie.Please explain what all things are passed through a context.Like when constructing an object for the following class..
public class myclass{
public myclass (Context context){....}
}
You get a lot of possibilities to check for or change System or application properties.
You will find a detailed version of all the functions that are available with the context in the api documentation of android:
http://developer.android.com/reference/android/content/Context.html
So you will be able for example to start a service (to run part of the application in the background) through context.startService(Intent service). You'll need to pass an Intent (if you don't know what an intent is I would read the Dev Guide: http:**developer.android.com/guide/topics/fundamentals.html first. You could do that anyway, there are plenty of good descriptions and examples.).
Sorry for the crippled link, I'm not allowed to post more than one link per post...

Categories

Resources