Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why two parameters to ComponentName constructor?

From the Pro Android 2 book: "ComponentName wraps a package name and a class name together. For example..."

Intent intent = new Intent();
Intent.setCompnonet(new ComponentName(
    "com.android.contacts"
    ,"com.android.contacts.DialContactsEntryActivity");
startActivity(intent)

If you look at the example, you'll notice that the package name can be easily derived from the class name. So the obvious question is: why two parameters? Why not provide only the class name?

Is there a scenario where the class passed to the ComponentName constructor doesn't belong to the package passed to the same constructor?

like image 851
Android Eve Avatar asked Feb 25 '11 21:02

Android Eve


People also ask

What is the use of Componentname in Android?

Create a new component identifier from a Context and Class object. Context : A Context for the package implementing the component, from which the actual package name will be retrieved. This value cannot be null .

What is the component name?

Each component has a label on it which can be changed for another one. As you may have many components in your graph and they may have some specified functions, you can give them names according to what they do. Otherwise you would have many different components with identical names in your graph.


1 Answers

The application component may exist within an application whose package name (declared in its Android manifest) is completely different from the Java package for the particular class that defines the component. An example is the MapsActivity in the Google Maps application:

intent.setComponent(new ComponentName("com.google.android.apps.maps", 
    "com.google.android.maps.MapsActivity"));
like image 77
Jeff Gilfelt Avatar answered Nov 06 '22 03:11

Jeff Gilfelt