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?
Asked
Active
Viewed 958 times
1
-
2easy 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. – njzk2 Nov 18 '15 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. – dramzy Nov 18 '15 at 18:57
-
start an activity in the background? that sounds more like a Service. – njzk2 Nov 18 '15 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. – dramzy Nov 18 '15 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. – NoChinDeluxe Nov 18 '15 at 19:12
-
The app is very much dependent on GPS location, there's nothing for the user to do inside the app without knowing their GPS location. It's similar to Yik Yak if you're familiar with that app. – dramzy Nov 18 '15 at 19:15
-
The splash screen basically displays as an `AsyncTask` is running in the background determining the user's location. Once that is done, I populate a `ListView` in my "main" activity based on the result of that `AsyncTask`. – dramzy Nov 18 '15 at 19:35
-
No need for an `AsyncTask`. Rely on Google's API Client to handle threading. All you have to do is implement its callbacks. They've done all the hard work for you. – Michael De Soto Nov 18 '15 at 19:48
-
I am not using Google's API Client, but I need to wait for a location fix before doing other stuff any way, would the API Client obviate that need? – dramzy Nov 18 '15 at 19:50
-
It's still asynchronous, so It wont alleviate the need to have to wait for a GPS fix. But, good news: It's tied into the whole OS. So if the device has a location already, you can use that without having to poll for fresh data. That call is synchronous I believe. Do: `LocationServices.FusedLocationApi.getLastLocation(googleApiClient);` But I recommend using a combination of the two. Start up the client. See if there's a prior location. Start up polling if there's not. And no matter what client you use, make sure you unregister your listeners or you will get a memory leak! See my answer below. – Michael De Soto Nov 18 '15 at 19:52
-
Currently, I register for location updates from both the Network and GPS providers, you're suggesting that I do that only if I can't get a location from `GoogleAPIClient`, correct? – dramzy Nov 18 '15 at 20:00
-
Nope. Google's API Client takes care of it all for you. I've found the location stuff to be the most confusing part of their docs. Mainly because they've changed the pattern at least three times over the years, and for a while, left all the docs up. But here's the secret: Take a look at [Getting the Last Known Location](http://developer.android.com/training/location/retrieve-current.html) and [Receiving Location Updates](http://developer.android.com/training/location/receive-location-updates.html). They're two pretty short articles that have all the code you'll need. – Michael De Soto Nov 18 '15 at 20:08
-
Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/95488/discussion-between-respect-my-authoritah-and-michael-de-soto). – dramzy Nov 18 '15 at 20:08
1 Answers
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.

Michael De Soto
- 1,238
- 12
- 24
-
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. – dramzy Nov 18 '15 at 19:57