8

I have a main activity, and it summons another activity to display some data.

I have a private database helper object that I use throughout the main activity code. Is there a way to pass the context of my main activity to my sub activity in an elegant way? (ie, from subclass, something like getCallingActivityContext())

I could always create new database helper objects.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Tony Ren
  • 83
  • 1
  • 1
  • 3

2 Answers2

6

Extending the Application class helps you to allow declare/access global variables. You can set your variables from any activity to ApplicationContext and access it from other activity without using bundle.

How to declare global variables in Android? will help you.

Community
  • 1
  • 1
gtiwari333
  • 24,554
  • 15
  • 75
  • 102
4

For your case, I would recommend you to extend instead the Application class. All your activities have access to the Application context by calling getApplicationContext() at any time. Check here the 1st answer for an example.

Community
  • 1
  • 1
Jan S.
  • 10,328
  • 3
  • 31
  • 36
  • You would be better linking to [the answer](http://stackoverflow.com/questions/708012/android-how-to-declare-global-variables/708317#708317) rather than the question – Moog Oct 13 '11 at 01:40