1

So, I have an app with a splash screen that starts another activity once it's done loading. When the user hits the home button while it's loading, the application goes to the background and then comes back to the foreground once that activity is started. Is there any way to stop that from happening?

13
  • 2
    easy solution but not an answer: don't have a splashscreen. More seriously, you can check the status of your activity before starting the next. Or you can cancel your loading when the splash is stopped. Commented Nov 18, 2015 at 18:54
  • I would rather not have a splash screen but I have a need to mask the initial delay of getting a fix on the user's location. I want to start the activity but in the background without it popping back into the foreground. I was hoping there is a simple intent flag to do so instead of programatically meddling with the activity's state. Commented Nov 18, 2015 at 18:57
  • start an activity in the background? that sounds more like a Service. Commented Nov 18, 2015 at 19:00
  • It's not a service, just a normal activity, but I want the user to be able to return to the app and have it already loaded, but also prevent the app from popping back up after the user has hit the home button. Commented Nov 18, 2015 at 19:01
  • From a design standpoint, Google suggests you do background tasks like location detection while the app is running -- as in, continue to allow the user to use the app while background tasks are doing their thing. Splash screens are really more for loading assets that are integral to displaying anything on the app at all. So you'd be better off putting in a progress bar or "determining location" dialog or whatever so the user can be in the app, but know that they need to wait a moment. This would also solve your issue above. Commented Nov 18, 2015 at 19:12

1 Answer 1

1

Two things:

First, make sure you're tearing down the reference to the location service in onPause. I assume you're using Google's API Client. If you're not, you really should be. So in onPause, make sure you unregister the listeners:

@Override public void onPause() { // Tear down Google API Client. if (googleApiClient != null) { if (googleApiClient.isConnected()) { // Turn off location polling. LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient, this); } // Avoid leaks by making sure callbacks get unregistered. googleApiClient.unregisterConnectionCallbacks(this); googleApiClient.unregisterConnectionFailedListener(this); googleApiClient.disconnect(); } } 

BUT: I think you're over thinking the solution on this one. Why have two activities? Why not one, and have a "wait state" until you get the location fix? Your wait state could be anything. Or a full screen splash as you say (Use a RelativeLayout and stack the views). When you get the fix, fade out the splash.

Then stash the location to the savedInstanceState bundle. When your activity's state changes, you'll know to not display the splash again.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, overlaying the splash screen on top of the main activity seems like the right way to go and thanks for the tip on using a GoogleAPIClient.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.