What is the use of Context to start an activity in Android? - android

When starting another activity, the method startActivity(someintent) is used, that intent, at the same time, contains the parameters this for context and a class object.
What is the use of the this parameter, given that the context is already known since the activity itself is a subclass of context? And, given that I create an intent as:
Intent myIntent = new Intent(this,someclass.class);
Am I only able to use the method startActivity when inside this context specified when creating myIntent. In other words, I can't use startActivity with the same intent from another activity.

Several reasons:
You are not required to use this as the context when creating a new Intent in an activity. (You might, for instance, create a ContextThemeWrapper to apply a separate theme.)
The Intent constructor can be called from outside an Activity. In any event, the Intent constructor has no way of knowing what object is calling the constructor (if any—it could even be called from a static context).
There are other uses for an Intent besides calling startActivity().

Related

Intent: Avoiding memory leaks when one instantiates an Intent, by using getApplicationContext()?

In the official documentation (https://developer.android.com/reference/android/content/Intent.html#Intent(java.lang.String,%2520android.net.Uri for the method public Intent (Context packageContext, Class<?> cls), we can read:
packageContext Context: A Context of the application package implementing this class.
This Stackoverflow answer What's packageContext in Intent#(Context packageContext, Class<?> cls)? precises what this definition means:
You can pass any Context object you got from any of you application's classes. So you can either use an Activity, Service object or you can call getApplicationContext() and pass the returned Context object to the Intent constructor.
Even if an Intent is something very shorted-live, should we use getApplicationContext instead of getActivity in order to avoid memory leaks (so that we avoid to keep the reference to the activity, which could be wanted to be removed from RAM)?
Example, in a fragment class, called after the activity was created to avoid a NullPointerException:
startActivity(new Intent(getActivity().getApplicationContext(), SplashScreen.class));
Doesn't matter, the context is not "held" by the Intent. It's just used to look something up (iirc, the name of the package).

Why is 'this' passed while creating an instance of Intent?

I am new to android app development.
I am trying to understand what is intent and its uses.
My question is that while starting another activity, why is 'this' keyword passed as the context parameter for the intent?
Intent foo = new Intent(this, viewContacts.class);
I understand that the any activity extends Context class, but why is it that we are passing the activity context and not the application context?
My Point-
When another activity starts the current activity will get destroyed but its context will be passed to the other one. Referring to this article, it says that
The most obvious way of avoiding context related memory leak is to avoid escaping the context outside of its own scope.
So aren't we passing the context of current activity to another one where the first one goes out of scope?
Isn't it an example of memory leak?
why is it that we are passing the activity context and not the application context?
Either would work here. this is less typing and faster to execute than is getApplicationContext().
When another activity starts the current activity will get destroyed but its context will passed to the other one.
You are assuming that the Intent holds onto this Context. It does not.
So aren't we passing the context of current activity to another one where the first one goes out of scope?
No.
An Intent can either be implicit or explicit. An explicit Intent is one that has a ComponentName attached, identifying the specific app (by package name) and Java class (by fully-qualified class name) of the component for which this Intent is intended. The two-parameter constructor, providing the Context and Class object, is used to build that ComponentName. Neither the Intent nor the ComponentName hold onto the Context after the constructor work is completed.

Is this a strange way to create an Intent?

I have a Library Project and a project each for the free and paid versions for one of my apps. In the Library project I have a 'Base Activity'. This 'Base Activity' must start child activities depending on whether it's the free or paid version.
What I did was just before I fire the intent with startActivity() I call a method in 'Base Activity' which must return the intent. I override this method in my 'Base Activity' subclasses (the paid and free versions) and create the intents like this:
return new Intent(subClassOfBaseActivity.this, ChildClassA.class);
and:
return new Intent(subClassOfBaseActivity.this, ChildClassB.class);
Now, my question is, it is ok to create the intent by passing it subClassofBaseActivity.this instead of BaseActivity.this ?
Is this method ok overall?
When you create a new Intent you need to pass the Context and the Activity.
Now the context is an interface which allows the application to access some resources.
Basically in your case , you're passing a context and activity as it supposed to, but you ask if you need to pass the baseActivity as the context.
So basically I think that because of this line in android developer:
Interface to global information about an application environment you need to pass the base activity as the context..
on the other side, the subClasses are probably inherit from the baseClass so the context should be the same, but it will be more readable and clear when you passing the baseClass as the context.
for more information about Context
http://developer.android.com/reference/android/content/Context.html
Yes, it is ok. This constructor take a first parameter Context which Activity extends. So basically you are passing argument as Context not as an Activity.
Thats fine.
Java will cast the first argument into Context (Activity extends Context) so it doesnt matter. You can also put getBaseContext() insteat of Activity.this there..

Android, trying to call a new intent from a touch event

I'm trying to make a app that when the screen is touch it will call up a new intent.
I have code that catches the touch event in the view class. When I try to create a new intent, Intent(this, cYesNoDisplay.class);, i get a error saying the constuctor is undefined, I'm assuming the constructor is not defined in the view base class, but the Activity class?
I'm confused about how to do this, is there a way for my View class that is a member of the intent class, to call it some how???? I figure there must be a wy to do this, still learning Java.
Ted
Your assesment about the View class that you are inside of being the problem is correct. In order to get it working do this:
Intent i = new Intent(NameOfYourActivity.this, cYesNoDisplay.class);
replace [NameOfYourActivity] with the name of the activity that you are inside of.
EDIT: I might have misunderstood what you were doing. If you have actually built your own View class and are overriding onTouch() you actually need to do it a little bit differently.
If you don't already have it add:
Context ctx;
to your classes declarations.
in your constructor alter it to store the context that gets passed in as a parameter in the ctx reference that you declared.
public [ClassName] (Context c){
this.ctx = c;
}
Then inside the onTouch() do it like this:
Intent i = new Intent(ctx, cYesNoDisplay.class);
ctx.startActivity();
EDIT again: The reason you have to use ctx.startActivity(i); is that startActivity() is a method of Context. Since Activity is a Context you don't have to put anything in front of it when you call it from inside an activity. But when you are "inside" of a different class you have to use a reference to a Context to call the method.
Use this . this work fine in my project.
Intent i = new Intent(NameOfYourActivity.this, cYesNoDisplay.class);
startActivity(i);

Testing an activity and pass an extra?

I am writing a test for my activity
MyActivity activity = new MyActivity();
activity.onCreate(null);
my activity expects and extra parameters to be passed, so when I call onCreate it crashes.
How can I pass an extra to my activity without using an Intent?
You could have the activity under test use some default values for the extra parameters, but I don't recommend that approach. Instead, just have the test pass in the values.
From http://developer.android.com/reference/android/test/ActivityInstrumentationTestCase2.html#setActivityIntent(android.content.Intent)
Call this method before the first call to getActivity() to inject a customized Intent into the Activity under test.

Categories

Resources