Reputation: 3337
This question is based on this older one (not by me).
I need to run the following code on my main activity (the one that launches first when I open my app):
public static boolean isInFront;
@Override
protected void onResume() {
//isInFront = true;
}
@Override
protected void onPause() {
//isInFront = false;
}
The problem is, onResume() causes my app to crash when I open it. As I understand it, this is what should happen because onCreate() should be called for the first activity the app runs (I also have that function later in my code).
But I really need this bit of code there to tell me when the main activity is the one currently active/in the foreground.
Is there any way to fix this so that I can keep onResume() in my main activity without running it instead of onCreate() when the app starts? Many thanks!
Upvotes: 1
Views: 7069
Reputation: 3929
You can have both methods, it's your code that is crashing it. You should check out the activity lifecycle.
Is that your only code? Do you call super.onResume() and super.onPause() in their methods?
public static boolean isInFront;
@Override
protected void onResume() {
super.onResume();
isInFront = true;
}
@Override
protected void onPause() {
super.onPause();
isInFront = false;
}
Sorry if I misunderstood your question
Upvotes: 4