The Intent class had 6 constructors
Intent()
Create an empty intent.
Intent(Intent o)
Copy constructor.
Intent(String action)
Create an intent with a given action.
Intent(String action, Uri uri)
Create an intent with a given action and for a given data url.
Intent(Context packageContext, Class cls)
Create an intent for a specific component.
Intent(String action, Uri uri, Context packageContext, Class cls)
Create an intent for a specific component with a specified action and
data.
I'm almost new in android programming and mostly using the fifth one when i need to start another Activity or Fragment:
Intent(Context packageContext, Class<?> cls)
When i want to start an Activity from a Fragment i do this:
Intent i = new Intent(getActivity(), DestinationActivity.class);
as far as i know, getActivity() will return an Activity
But the constructor expect a Context, how is this possible???
is it possible because of that the Activity that had returned by getActivity() implicitly invoke getApplicationContext()???
Take a look at the argument Context very closely in the fifth Intent declaration. It reflects polymorphism. The Intent takes a Context argument so you can pass any object that is a Context or derives from the Context class.
Activity, AppCompatActivity, IntentService, Service all derive from the Context class and hence can be passed as an argument to the method.
Activity inherits context. Thus, if you are in an activity, you only need to pass itself to use the context. It also contains a pointer to getBaseContext(). You might occasionally need to reference that, if you need the entire application context, but most likely you won't for a while.
You can find more details about the Activity class here.
This question about the intent constructor parameters is similar to yours and has a really good answer. I think you'd like to check it out.
Hope it helps.
Activity extends Context so you can just cast it:
Intent i = new Intent((Context)getActivity(), DestinationActivity.class);
Related
I was building an android app, and I needed to pass the current instance of a class to a service that I am launching from that class. So, like we bundle strings or many other datatypes to intent, something like this,
Intent intent1 = new Intent(getApplicationContext(), AlarmScreen.class);
intent1.putExtra("remainder", "example1");
intent1.putExtra("time", "example2");
startActivity(intent1);
Is there a similar way to bind context to intent and then pass it to the service to access the class's current instance of variables from the service class?
EventBus worked, being a singleton class, it does not create duplicate instances.
How can i launch an AndroidAnnotations Activity_ inside of an App (main)
since external Activity (another App).
this is my current code:
Intent codeScannerActivity = new Intent(PACKAGE, CODE_SCANNER_ACTIVITY);
codeScannerActivity.putExtra("codeScannerType", CameraUtils.CODE_SCANNER_SINGLE);
startActivityForResult(codeScannerActivity, Core.ActivityResult.RequestCode.CODE_SCANNER);
where PACKAGE = "main.app.package"
and CODE_SCANNER_ACTIVITY = PACKAGE + ".activity.MyActivity_"
but logs throws:
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=main.app.package dat=main.app.package.activity.MyActivity_ (has extras) }
Activity is defined in the Manifest's Main App with the Class "etc.MyActivity_".
You are constructing the Intent incorrectly. For the constructor you are using, the first parameter is interpreted as an "action" and the second as a URI. The error says that there is no activity which can respond to the action "main.app.package" and the URI "main.app.package.activity.MyActivity_".
To fix the problem, first read Starting Another Activity and the Intent javadocs from the Android Developer site. Especially look at the documentation for the available constructors. There might be one more appropriate for your purposes than the one you are trying to use. The Intent documentation has a list of standard Activity actions. If you want to start a specific activity, you should use Intent (Context packageContext, Class<?> cls):
Intent intent = new Intent(this, main.app.package.activity.MyActivity_.class);
I was creating wrong the Intent, this is the right way:
Intent codeScannerActivity = new Intent();
codeScannerActivity.setComponent(new ComponentName(PACKAGE, CODE_SCANNER_ACTIVITY));
codeScannerActivity.putExtra("codeScannerType", CameraUtils.CODE_SCANNER_SINGLE);
startActivityForResult(codeScannerActivity, Core.ActivityResult.RequestCode.CODE_SCANNER);
For what purpose Context class is used in android?Please explain me in depth and be more specific .I read all other posts but none of them were specific enough to give me clear understanding.
I know Content class allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.
Like Here,
Intent intent=new Intent(this,new_class.class);
why we are passing Main activity context into the Intent constructor call.what type of information does this activity context contain,how will it help it ,what type of resource access is it providing to it ?(with example please).
Similarly,
here,
TextView textview=new TextView(this);
Why TextView need activity context?How does it help it.
There are already several good explanations of Context on Stackoverflow (see the linked questions and us the "search" feature. Also, the source code for Android is available on grepcode.com and you can look yourself if you are really interested. Why trust someone else's answer if you can look yourself? ;-)
However, I will answer your specific questions:
Intent intent=new Intent(this,new_class.class);
why we are passing Main activity context into the Intent constructor
call.what type of information does this activity context contain,how
will it help it ,what type of resource access is it providing to it
?(with example please).
In this case (the 2-argument constructor for Intent), the Context parameter is only used to determine the package name of the target Activity. Assuming that the package name of your application is "com.example.app", and MyActivity is an `Activity of your application, the following code snippets are all functionally identical:
Intent intent = new Intent(this, MyActivity.class);
Intent intent = new Intent(getApplicationContext(), MyActivity.class);
Intent intent = new Intent();
intent.setComponent(new ComponentName(this, "com.example.app.MyActivity");
Intent intent = new Intent();
intent.setComponent(new ComponentName(this, MyActivity.class);
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.example.app", MyActivity.class);
Intent intent = new Intent();
intent.setClass(this, MyActivity.class);
Intent intent = new Intent();
intent.setClassName("com.example.app", "com.example.app.MyActivity");
Similarly, here,
TextView textview=new TextView(this);
Why TextView need activity context?How does it help it.
All Views need a Context. Think of the Context as the "owner of the View". This controls the lifetime of the View. In general, a View should have the same lifetime as its owning Activity, which is why you usually pass the Activity as the Context parameter when creating a View. When the Activity is destroyed, all of the owned Views are also destroyed. Additionally, the View uses the Context to gain access to resources (drawables, layouts, strings, themes, etc.).
It act as a Interface to global information about an application environment. This is an abstract class whose implementation is provided by the Android system. It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.
You can get all the information from delveloper's official documentation... https://developer.android.com/reference/android/content/Context.html
I am trying to learn how to develop Android applications. I am reading on the Android Developers Guide site http://developer.android.com/guide/components/intents-filters.html, specifically about Explicit vs. Implicit Intents. One of the examples for an explicit intent looks like this:
// Executed in an Activity, so 'this' is the Context
// The fileUrl is a string URL, such as "http://www.example.com/image.png"
Intent downloadIntent = new Intent(this, DownloadService.class);
downloadIntent.setData(Uri.parse(fileUrl));
startService(downloadIntent);
The constructor Intent(this, DownloadService.class) looks like this public constructor in the Android APIs ( http://developer.android.com/reference/android/content/Intent.html ):
public Intent (Context packageContext, Class<?> cls)
The Intent constructor documentation states:
Create an intent for a specific component. All other fields (action, data, type, class) are null, though they can be modified later with explicit calls. This provides a convenient way to create an intent that is intended to execute a hard-coded class name, rather than relying on the system to find an appropriate class for you; see setComponent(ComponentName) for more information on the repercussions of this.
What I am trying to figure out is, what is the "this" in the constructor? I mean, I know what "this" is in general (the current instance of your activity), but what is it in this specific content? In the comments, it says "Executed in an Activity, so 'this' is the Context", but what is the "Context"? It isn't one of the five pieces of information that make up an intent (Component Name, Action, Data, Category, Extras, Flags). I know the "DownloadService.class" parameter is the Component Name, so I'm just trying to figure out what it is.
What I am trying to figure out is, what is the "this" in the constructor?
It is an instance of some subclass of Context, such as an Activity.
It isn't one of the five pieces of information that make up an intent (Component Name, Action, Data, Category, Extras, Flags).
No, but the combination of the Context and the Java class is enough to build the ComponentName. A ComponentName is a combination of an application ID and the fully-qualified class name to the component in that application. The Java class can provide the fully-qualified class name; the Context is where ComponentName pulls in the application ID (in this case, for your own application).
When creating a pending intent using the method PendingIntent.getActivity(), the first argument is (according to the android docs)
The Context in which this PendingIntent should start the activity.
When im lanuching a new instance of an activity in my application, i can pass getApplicationContext(). But if i want to launch an instance of another application's activity, what should be the context argument ?
This context is needed for startActivity(), so just pass what you did so far and it shall make no difference that the activiy you want to launch is in other package.
Just this context is OK. It is similar as you launch the activity by getApplicationContext().startActivity(intent) in your app.
You can use any Context you like to fire an Intent to another Application - Whether that be a Service, an Activity or a Context from getApplicationContext() or passed to you in a Receiver.