Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android use component name to launch an activity

There are many ways to start another activity. The most of the overloading methods requires you to pass a context.

But when using componentName to launch an activity using

public Intent setComponent (ComponentName component)

and this constructor for ComponentName

ComponentName(String pkg, String cls)

You see above, I am able to launch an activity WITHOUT using any Context argument

But it must use some "context" somehow internally, am I right? If so, which context? Application one or the activity one? Does this mean that every time I use this two methods (above), I do not need to worry about memory leak becuase I am not passing any context around??

Thanks

like image 338
user1118019 Avatar asked Feb 20 '12 18:02

user1118019


People also ask

How do I launch an application using package name?

Just use these following two lines, so you can launch any installed application whose package name is known: Intent launchIntent = getPackageManager(). getLaunchIntentForPackage("com. example.

How do I launch an activity using intent?

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 Intentfilter?

An intent filter is an expression in an app's manifest file that specifies the type of intents that the component would like to receive. For instance, by declaring an intent filter for an activity, you make it possible for other apps to directly start your activity with a certain kind of intent.


1 Answers

You don't have to worry about memory leaks in either case, but it's good that you're keeping an eye on where you're passing Context objects. Intent simply uses the Context parameter to look up your package name when you use the Intent(Context, Class) constructor or setClass(Context, Class) method. They're just convenience methods.

like image 170
adamp Avatar answered Sep 17 '22 05:09

adamp