3

I have a splash screen which handles automatic login. If the user is logged in, it sends s/he directly to the main activity, through the following code:

Intent intent = new Intent(activity, MainActivity.class);
activity.startActivity(intent);
activity.finish();

Where activity is the activity to be finished (I'm not using this because this method is common to automatic logins, manual logins, social logins etc.)

However about 70% of the time, the splash screen is still there after finishing the main activity. This does not occur during debugging. It looks like it happens when the login happens quickly, which is why it probably never happens during debugging (because everything becomes slow).

Logging has helped my see that, when the problem happens finish() is indeed being called, but onDestroy() is not.

I have tried FLAG_ACTIVITY_CLEAR_TOP, but it doesn't help. noHistory does work, but brings an unwanted finishing animation and, really, I want to know what the problem with this code is. I've always used it and don't see why it fails.

Thanks in advance.

Pablo
  • 652
  • 6
  • 21
  • please provide the loginActivity code. How are you verifying that you have logged in... If you are using some sort of eventbus then make sure you have registered before you make the login attempt. – toidiu Apr 22 '15 at 19:23
  • I don't know what you mean by event bus or loginactivity, but the login is verified by a separate class which receives the reference to the splash screen in its constructor, makes a network call using the Volley library and the associated callback checks the server's answer to see if it is okay. If it is, it runs the code shown above. – Pablo Apr 22 '15 at 19:32
  • Interesting, though difficult to investigate without context. If possible, can you provide a minimal project with splash activity etc.? – Melquiades Apr 22 '15 at 19:50
  • I'll do as soon as I can. For now, it seems like part of the problem has gone away. I switched branches and, when I came back, now the annoying animation doesn't happen anymore. I have no idea why. So, for now, I'm sticking with `noHistory`. – Pablo Apr 22 '15 at 19:58
  • Who is calling the login class? "receives the reference to the splash screen in its constructor" It seems like you are passing the view reference to the login class. I would advise against this. Instead add a callback for the login class that calls code in the Splash activity. The Splash activity can then handle the successful. – toidiu Apr 22 '15 at 20:18
  • You say you're not using `this` - I don't understand why you're not using `this`. Are you keeping references to `Activities` in other `Activities`? If so, that doesn't sound too healthy. Keeping references around in other places can cause the `Activity` you want to `finish()` to not be finished. I'm using the exact same code in an app like this: `Intent autoLoginIntent = new Intent(this, MainActivity.class); startActivity(autoLoginIntent); finish();` and it's working great. – Darwind Apr 22 '15 at 22:07
  • Hi guys. I'm not closing it from another Activity. Both Activities (Splash and Login) create an instance of a SessionStartController, which is responsible for all kinds of things related to this login. It's not an activity, so I don't see the problem. The reason for its existence is because both activities share a lot of common code. No activity exists while another one is running. – Pablo Apr 23 '15 at 17:23

0 Answers0