0

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?

Koded101
  • 375
  • 4
  • 11

3 Answers3

1

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.

andrew
  • 6,533
  • 1
  • 22
  • 19
1

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..

danijoo
  • 2,823
  • 4
  • 23
  • 43
1

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

Elior
  • 3,178
  • 6
  • 37
  • 67
  • So as danijoo suggests using getBaseContext() would be better/clearer? – Koded101 May 14 '13 at 20:58
  • I would suggest to use baseClass.this instead getBaseContext, you can read about it here http://stackoverflow.com/questions/1026973/android-whats-the-difference-between-the-various-methods-to-get-a-context anyway there is a method called getParent() this return the context of the parent activity, if the current view (activity) is a child of the parent activity – Elior May 14 '13 at 21:04
  • @Koded101 Checking the source code of Android I found that this `Context` is used just to get application package name, so I assume you are free to use any solution. I don't think there is suggested way to pass context to `Intent` constructor, just for creating Intent use the way you think it is clearer. Actually I don't see any confusing stuff by passing `AnyActivity.this`. – andrew May 15 '13 at 06:43