My app starts with a splashActivity. In my Application, I start all my Singleton classes, one of them is ActivityController (Simplified code):
public class ActivityController {
private static ActivityController INSTANCE = null;
private final String TAG = "ACTIVITY MANAGER";
static Context context;
private ActivityController() {
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
private synchronized static void createInstance(Context c) {
if (INSTANCE == null) {
context = c.getApplicationContext();
INSTANCE = new ActivityController();
}
}
public static ActivityController getInstance(Context c) {
if (INSTANCE == null) createInstance(c);
return INSTANCE;
}
And this is my Application class:
public class Global extends MultiDexApplication {
@Override
public void onCreate() {
super.onCreate();
ActivityController.getInstance(this);
JodaTimeAndroid.init(this);
//etc...
}
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(newBase);
MultiDex.install(this);
}
}
If I run this code, it starts MainActivity, but when I close the app (sliding finger), it restarts after few seconds. Always. It's impossible to close it.
The only way I've found to not have this problem is to start the ActivityController.getInstance(this)
in the OnCreate
of the SplashActivity, which is the launcher Activity. This way I can close the app and it won't restart.
Why is this happening? I think it's related with "context".
I've read this without any hint: Activity restarts on Force Close