6

While working with different activities and starting them for results I have no choice but to use intent. Now intent requires context and that makes no sense to me. I know that context allows access to the application resources but

why do you need to know about the application resources when an intent is just a simple messenger?

Also, I am not so sure why some people create intent with the getApplicationContext() while other use this for the activity context????

Lastly, I am not so sure how the activity that calls for startActivityResult() receive a call back on the method onActivityResult() when I don't pass the "this" for the context but instead the application context. I thought that you have to use the "this" or passing in the current activity context that called startActivityResult() in order to receive a callback. That is just straight up java right? If you pass in a class then the other activity class will have a reference to your class and hence allows it to call the method in your class which is onActivityForResult(). However, this is not the case so what am I missing?

Wowzer
  • 1,103
  • 2
  • 12
  • 26

3 Answers3

8

Intent itself does not need the Context. The constructor Intent#Intent(Context, Class) is just a convenience constructor, that internally uses the provided arguments to derive a ComponentName. ComponentName is in turn just a package name of your app and a class name to target. So ComponentName might be something like:

com.foo.bar/com.foo.bar.ui.activity.MyActivity

However, you can as well just use an empty constructor Intent#Intent() and provide ComponentName yourself (Intent#setComponentName(ComponentName)).

Therefore it doesn't matter if you provide your Application's or your Activity's context (the latter is just simpler to type). Also keep in mind that classes that require application context can call Context#getApplicationContext themselves, so this is not something you need to worry about.

About startActivityForResult() - Android manages internally a stack of your Activity records. Therefore it delivers the result to the previous Activity on the stack. It is the same way it knows where to return, when you click "back".

Please note it doesn't mean it maintains a stack of your Activity instances. These instances might be long gone - destroyed and garbage collected to free the memory. However the stack contains the information that allows to recreate them and to restore their state.

Marcin Jedynak
  • 3,697
  • 2
  • 20
  • 19
  • Hi there your answer is very clear and concise. However, I just want a quick clarification. So whenever I start an activity, the activity that calls startActivity() will be register in the stack correct? So how does android knows to reference that activity and register it into a stack when I don't specifically pass in that activity. – Wowzer Dec 04 '16 at 09:36
  • The system knows which activity is on the top of the stack (your activity). And when you start a new one it uses a component name to identify it. Even if you use an implicit intent (one with action instead of an explicit target name) it is eventually resolved to a specific component name. – Marcin Jedynak Dec 04 '16 at 09:44
1

Intent does not need Context for itself but as you yourself pointed out that Intent is just a messenger. It also passes the current state of application/object to the newly created object so that it can understand that what exactly is going on in the application. And that is why we need to pass the context.

And, I believe that you want to ask about startActivityForResult(). Android itself takes care of the callback in the same way other callbacks are handled. You can take the example of Activity Life-cycle. Whenever it is started onCreate(), onStart(), onResume() are called itself by Android.

hittsss
  • 76
  • 5
  • I don't think that the context is used to understand what is going on in the application because when you launch the new activity, android will just give the context by default to that new activity because all activity inherits from the same application context provided that the activity is within the application – Wowzer Dec 04 '16 at 09:40
  • 1
    Android will not give itself the Application Context to the new Activity. There is a difference b/w using getAplicationContext() and using this. Android will provide the context whichever you will declare it to give. Refer to Question No. 1026973 – hittsss Dec 04 '16 at 09:52
  • @hittsss Can you provide some authorative sources that back up your explanation? – Mehdi Charife Feb 13 '23 at 21:30
0

Not been much deep into Android development but still let me try with an explanation. So basically, context is a reference to linking your resources to your program. Each object is given its own context, which contains the resources required to set that object up. It is required for many objects to be created, and to get program identifying information, among other purposes. This makes it invaluable to set up new views and activities, but it can also be used for other purposes. See this Android Context for more information.

According to this page 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.

There are ways of creating an Intent which do not require a Context. But if you want to target a specific class of a specific package, then providing a context for the target package is a ready way to do that. Refer this Context Lesson.

As explained by snctin in his answer getApplicationContext() offers application context. Basically the Application context is associated with the Application and will always be the same throughout the life cycle of your app. Also refer this post.

See Android - How to start (display) a new Activity. According to it

startActivity(new Intent(this, ProjectsActivity.class));

assumes your current class extends one of the Android Activity classes, which gives you access to the startActivity method.

According to Getting result from a activity, Starting another activity doesn't have to be one-way. You can also start another activity and receive a result back. To receive a result, call startActivityForResult() (instead of startActivity()).

For example, your app can start a camera app and receive the captured photo as a result. Or, you might start the People app in order for the user to select a contact and you'll receive the contact details as a result. This post will help you understand the same more better way.

Hope that helps. And also thanks becuase of your question I had a refresh with Android.:)

Community
  • 1
  • 1
StackUseR
  • 884
  • 1
  • 11
  • 40