0

I'm having a slightly odd problem, and I can't seem to find anyone with the same problem when googling although this may be because I'm not explaining it well.

In my app the user logs in, as soon as they hit log in a loading activity opens, after this the main page activity opens.

Now if the app is on the loading activity and the user presses the 'Home' button on their phone or goes to a different app my activity will still continue loading the information in the background.

As soon as loading is finished then the main page activity is launched, regardless of whether the app is still being used. This main page will then open on top of whatever the user was doing.

I am using an async task for the loading, with the following onPostExecute() method:

@Override
public void onPostExecute() {
    //open the main page  
    Intent mainPage = new Intent(getApplicationContext(), MainPageActivity.class);                             
    mainPage.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    mainPage.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK );
    startActivity(mainPage);
}

What would be the best way to stop this behaviour?

I want the main page activity to be ready to open/already opened next time the user brings the app to the foreground I don't want it to open on top of what they are doing.

Thanks for your time

user3707803
  • 101
  • 1
  • 11

2 Answers2

0

In your AsyncTask you are starting the Activity... you should send an intent to the Activity so that is responds appropriately if it is still running.

In other words, your AsyncTask calls startActivity and it should send the result to a Service if you do not expect the activity to be running. A Service will run even when an activity is not. Then you can register a listener on the service from an Activity if the user is in your app. See this post:

Notify activity from service

You will need to be sure that the listener does not start the Activity, but that should be easy to do.

Community
  • 1
  • 1
Jim
  • 10,172
  • 1
  • 27
  • 36
  • Sorry I'm new to android and I'm struggling with this. I had never heard of services before I read your post, but I'm not sure if I understand correctly...why can't the listener start the activity? thanks for your answer – user3707803 Nov 07 '14 at 11:15
  • As said by @cygery the AsyncTask are not the thing to be paused/resumed. Listener can definitely start/stop the activity but the point is you are not sure when your asynctask finishes its task and calls listener. So your problem remains same. whenever listener is called it will start your activity no matter your app is running or you are doing something else. you stand at same place where you were. hope you got it. – MOSO Nov 07 '14 at 11:55
  • I think so, I'm just completely out of my depth here. Do you think I'd be right in saying I need a Bound Service? – user3707803 Nov 07 '14 at 12:08
  • it depends how heavy task you are doing. if not so heavy i would have checked if my application has gone to background or not..if not fire the intent if yes remain dormant and probably save the data or whatever.. – MOSO Nov 07 '14 at 12:39
  • thanks for your help I just tried that suggestion and it works really well ! :) I don't know if I can upvote you in anyway though as this is a comment – user3707803 Nov 07 '14 at 13:53
  • @user3707803: hmmm I see you have low reputations. No probs wen you earn them just upvote :p kidding. Glad it helped in anyway. – MOSO Nov 12 '14 at 18:12
0

What would be the best way to stop this behaviour?

Make sure that you don't call startActivity when your Activity is not running. You could stop your AsyncTask in the onPause() or onStop() method of your loading Activity.

cygery
  • 2,309
  • 3
  • 18
  • 25
  • Is there a way to effectively 'pause' an async task? I don't want to completely stop it and restart it as loading can take a while - especially if they have a slow connection - so if they open another app to pass the time I don't want loading to restart when they come back to my app. thanks for your answer – user3707803 Nov 07 '14 at 11:13
  • AsyncTasks aren't designed to be paused/resumed. If you require that the login procedure runs in the background you could use a Service instead. – cygery Nov 07 '14 at 11:19