Reputation: 375
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?
Upvotes: 0
Views: 97
Reputation: 3266
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
Upvotes: 1
Reputation: 2843
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..
Upvotes: 1
Reputation: 6563
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
.
Upvotes: 1