0
public class MyTabActivity extends TabActivity {
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    dataManager = DataManager.getInstance(getApplicationContext());
  }
}

I have a tab activity as shown above. It works fine on the initial run. After some time in the background where it's activity is deleted from memory, when you reopen the app, it will crash. The reason is because getApplicationContext() returns null.

I use this same setup in other Activities with no problems. I can't find anywhere in the documentation that says when/why/if it would return null.

mouser58907
  • 797
  • 2
  • 10
  • 21
  • why dont you use `this` instead? – Ron May 20 '12 at 18:30
  • @userSeven7s because I need the applications context, not the activity's context. – mouser58907 May 20 '12 at 18:31
  • try `this.getApplicationContext()`... – Ron May 20 '12 at 18:34
  • getApplicationContext can be null if you are calling onCreate directly. How is the onCreate called? By the framework or by you? – kingston May 20 '12 at 18:37
  • @userSeven7s That is identical to what I have as far as the compiler is concerned. It gives the same result. – mouser58907 May 20 '12 at 18:37
  • @herschel it is being called by the framework. It is just upon re-opening the app. – mouser58907 May 20 '12 at 18:38
  • @mouser58907 : There's no reason why this should happen with `TabActivity` and not with `Activity`. The method `getApplicationContext()` is inherited from `ContextWrapper` and the only difference between `Activity` and `TabActivity` is that `Activity` inherits directly from `ContextThemeWrapper` whereas `TabActivity` inherits directly from `ActivityGroup` (which inherits directly from `ContextThemeWrapper`). The `ActivityGroup` class doesn't override `getApplicationContext()` so there isn't any explicit difference. – Squonk May 20 '12 at 18:58
  • @MisterSquonk I agree, I don't think it has anything to do with it. But I'm having a hard time tracking it down because I can't get the debugger to stay connected after I leave the app. – mouser58907 May 20 '12 at 19:15

1 Answers1

0

You can simply pass "this" to your DataManager.getInstance because your MyTabActivity inherits from Context (three levels up)

Simone Casagranda
  • 1,217
  • 17
  • 26
  • I need the Application's Context though so it will persist. You can read about the differences here: http://stackoverflow.com/questions/4128589/difference-between-activity-context-and-application-context – mouser58907 May 20 '12 at 18:59
  • @mouser58907 : You don't need it to 'persist' anything - as soon as your `TabActivity` is destroyed, your `dataManager` variable will no longer be valid. All you need is a 'current' valid `Context` for the lifetime of the `TabActivity` and the `dataManager` object. In that case use `this` because your `dataManager` object is only created in the `onCreate(...)` method and will be no longer relevant anyway when the `TabActivity` is destroyed. – Squonk May 20 '12 at 19:06
  • @mouser58907, MisterSquonk has just answered for me :) – Simone Casagranda May 20 '12 at 19:08
  • 1
    @MisterSquonk My dataManager is a singleton that registers a broadcast receiver. Therefore it does need the application context. This is the recommended implementation as stated here: http://developer.android.com/reference/android/app/Application.html – mouser58907 May 20 '12 at 19:11
  • @mouser58907 : Yes, sorry I realised that after I'd posted but haven't had a chance to come back. It seems to me it would be better to have your `dataManager` object maintained by a class which extends `Application`. You haven't explained what your `BroadcastReceiver` does but if it is needed to monitor system intents, for example, just register it with the relevant `` in the manifest. – Squonk May 20 '12 at 22:29