-5

I'm fairly new to android and having an issue grasping the concept of Contexts and all.

I have a small app that consists of 3 activities:

1- Main Activity

2- Login Activity

3- Splash Activity

I've created a number of classes that are part of MainActivity and run as part of it. One of the classes is a database helper. Database helper needs a context to use and what I do is, in MainActivity:

public static Context context;

and in onResume I do:

@Override
public void onResume() {
    super.onResume();

    this.context = this;
}

All activities work just fine, spalsh works, login works, everything works fine. I've defined everything in Manifest.

But randomly when I leave my app running in background for a while and do other stuff with my phone and open it up, sometimes it just crashes pointing to a function that gets "context" as an argument.

Is there anything I can do to fix this?

Thanks

Arn
  • 600
  • 1
  • 6
  • 22

4 Answers4

2

Avoid having static references to Context since they could cause memory leaks, you can read this blog article about it. Since the activity is a context itself you can pass it to other classes with ActivityName.this.

Jameido
  • 1,344
  • 1
  • 11
  • 21
2

Application crashed as you used only "this" keyword to assign a reference. "this" keyword points to current object. When application in background, "this" can refer to another object. So to avoid crash bind the keyword "this" with activity name like "MainActivity.this" where MainActivity is your current activity name.

Anshika Bansal
  • 320
  • 2
  • 9
1

Use ActivityName.this replace of context.Your problem will be solved.

Vishal Vaishnav
  • 3,346
  • 3
  • 26
  • 57
  • How this and ActivityName.this is different if he used ino onresume? – Pankaj Kumar Jun 06 '17 at 06:58
  • I've tried using MainActivity.this but I get the error: Error:(25, 31) error: not an enclosing class: MainActivity I'm not sure how to fix that either....any help? – Arn Jun 06 '17 at 07:03
  • I can understand, asking a question makes you angry and this group voting will not work very long :) – Pankaj Kumar Jun 06 '17 at 07:04
  • Also why is it a bad idea to have context static? – Arn Jun 06 '17 at 07:08
  • @Arn Read accepted answer https://stackoverflow.com/questions/10258507/is-it-safe-to-save-the-app-context-to-a-static-variable-in-android – Pankaj Kumar Jun 06 '17 at 07:13
1

This is happening because the activity who called that background task is not alive at the time of crash. So you need to use Application Context there.

Easy way to do that is

@Override
public void onResume() {
    super.onResume();

    this.context = this.getApplicationContext();
}

And yes Context as you defined (public static Context context;) should not be static. (Although this is not causing problem to you, Best practice is to not to make Context as static.)


You should read about What is 'Context' on Android? and App crashes because of "context" issue.

Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186