0

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

Community
  • 1
  • 1
Borja
  • 1,269
  • 1
  • 17
  • 30
  • Is onCreate of your Application being called? Throw some breakpoints or logs in to figure out what is getting called when. – zgc7009 Jul 13 '16 at 15:07
  • @zgc7009 Yes, it's being called, each time it's restarted – Borja Jul 13 '16 at 15:11
  • Are you sure it isn't getting called which is causing your activity to launch when you call getInstance()? – zgc7009 Jul 13 '16 at 15:13
  • @zgc7009 I don't understand you at all, it's called 1 time, when app is launched – Borja Jul 13 '16 at 15:26
  • Let's start from the beginning, why are you doing this? What is your "ActivityController" for? – zgc7009 Jul 13 '16 at 15:31
  • @zgc7009 ActivityController will start X activity depending on some parameters that I've omitted to simplify code, I have this controller to avoid putting that code in SplashActivity, which is meant to be part of View, and should not contain this kind of code, I think. – Borja Jul 13 '16 at 15:33
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/117234/discussion-between-zgc7009-and-borja). – zgc7009 Jul 13 '16 at 15:36
  • We are working on moving this off of a static context reference – zgc7009 Jul 13 '16 at 15:45

0 Answers0