Dan
Dan

Reputation: 724

How do you stop a certain Activity from being resumed when you relaunch an app from the homescreen?

My Android app has a main activity, where a user clicks a button, which starts a "loading activity" with a spinner, which starts the results activity when finished loading.

The loading activity is simply an onCreate function that spawns a new thread that fetches information from the web.

Often times, after using my app for a while, the loading activity will be the one that is shown when resuming my app from the homescreen. The activity runs forever, spinner spinning the whole time. The only thing I can do is press the home key to close the app (and the problem remains until force closing the app). How do you prevent this? I want my main activity to start in these cases.

I've tried adding the following override function, but it didn't work:

@Override
public void onResume() {
    super.onResume();
    finish();
}

Upvotes: 0

Views: 382

Answers (5)

Anna Billstrom
Anna Billstrom

Reputation: 2492

I control the lifecycle of my app by having a method called "determineAction()", that checks various logical states. On the launch Activity, if it's valid, it continues loading, otherwise it redirects to other activities. So no need to stop or pause the activity, just reroute it to the place you want it to be.

Upvotes: 0

Will Kru
Will Kru

Reputation: 5212

Seems like a bad program structure and using finish like that will only make it worse. It seems your program hangs at certain times so you should focus on fixing that instead of hacking your way around. Check if your callbacks is being called, check if you aren't creating more loading processes than you need, check if they are not interfering, etc. To prevent your program from loading stuff once it's been loaded, you could simply set a (static) variable and check that before starting the loading process.

Upvotes: 0

Christopher Perry
Christopher Perry

Reputation: 39225

Don't load data in a separate Activity. This is what threads are for. Load your data in the background, and populate the UI in the relevant Activity when the data is returned.

You could use AsyncTask for this, or Loaders if you don't want to mess with threads.

Upvotes: 1

Bill Gary
Bill Gary

Reputation: 3005

Try putting finish() in onPause() of the activity you do not want to restart.

Upvotes: 1

Karakuri
Karakuri

Reputation: 38595

Have you considered using LoaderManager and LoaderCallbacks<> to implement your asynchronous loading? They added this to Android starting in Honeycomb specifically for things like what you're trying to accomplish.

http://developer.android.com/reference/android/app/LoaderManager.html

Without more specifics, it's hard to tell you the best way to approach this, but this may get you out of having a whole separate activity just to show a spinner while you load something.

Upvotes: 0

Related Questions