I have a app that uses webscockets, and when the app goes to backgroud i need after a x amount of seconds to disconnect the websckets and change the current activity to the lobby activity.
I have a singleton that extends Application and implements
ActivityLifecycleCallbacks, each activity is registered on registerActivityLifecycleCallbacks.
The problem is that when the app is in background and i call the lobby activity, the new activity calls all Activity Lifecycle Callbacks, like onActivityCreated, onActivityStarted, onActivityResumed and then onActivityStoped, and that behavior forces my to do a bunch of checks to see if the lobby activity is called on background or on foreground.
Is there a way to do this without that behavior, like to put the activity on hold ready to start on app resume?
And i prefer not to call the new activity when the app resumes it feels jerky, because you see the last open activity and then you see the change to the lobby activity.
Sorry for the long question.
You should use a Service to achieve that you want
Visit http://developer.android.com/guide/topics/fundamentals/services.html
Hope this will help :)
Related
I need my application to do 2 network calls when ever the app is started for the 1st time, when it has been killed and started again as well as when it has been placed in the background then returned.
I know I can put it in my "MainActivity" onCreate/onResume. I have a class that extends Application which is where I am initialising logging and crash reporting, but I noticed there is no onResume method, which from my understand is the method that is called when the application comes from the background.
Where would be the best place to do these 2 network calls to update certain aspects in my app when the app is started for the 1st time, started when killed and resumed when it comes back from the background.
FYI. The reason I don't want it to go in my "MainActivity" is that I don't want these network calls to be called when ever i return to the MainActivity from another screen in the app, only when the user returns to the app?
Thanks
The use case of calling whenever the app returns from background is by implementing Activity Life Cycle methods in your Application class:
public class myApp extends Application implements Application.ActivityLifecycleCallbacks {
...
}
In this case you make sure that whenever an onPause() is called a corresponding onResume() should also be called (i.e. normal screen switching). If not, then you know that your app is now in background. The next callback to onResume() should mean that it has come to foreground again and you can make your network call.
I have an activity that starts a service in its onCreate() method, and then programatically calls the home screen, that is, the onStop() method gets called on the activity, and it goes into background.
I want that same instance to come to foreground. From the things that i have searched, it looks like this can only be done manually, but i want to do this programatically, be it from the activity or the service.
I do not want to recreate another instance of the same activity by calling a new intent.
So basically the question is, i want to go to onStart() from onStop(), and this is perfectly possible by looking at the android lifecycle. I just want to do this programtically.
Thanks a lot in advance for your help.(:
My end goal is this:
MainActivity has a button that starts SubActivity (which is essentially a timer).
Each time the button is pressed a new SubActivity starts.
MainActivity also has a RecyclerView that displays a card for each SubActivity so that you may return to any timer that you have already started.
My problem is I don't know how to return to each Activity. I already did some research, and most people point towards onSaveInstanceState, but from my understanding that simply would recreate the activity by saving values and restoring them. I know my timer SubActivities still proceed in the background because even when I leave the SubActivity with the timer running, the alarm will go off later, but I am left unable to reopen that activity in the meanwhile.
So how do I access the running timer activity?
Thank you!
Not clear what you want to achieve really, but it looks like wrong app design. If you want timer to run regardless of activity then you should decouple timer from activity and make activity just display the timer. Your timers should be elsewhere and definitely its lifecycle should not depend of actvity lifecycle.
The question is more conceptual than coding-related.
I have an app with Activity A and B.
I call an AsyncTask from Activity A, while the call is being made, I don't want to block the user showing the progressdialog, so my user can freely move around the application without getting bored of waiting.
Now, the query is AsyncTask or lets say a Service is being called from Activity A which is responsible for downloading some kind of data from the server. While the call is being made, the user has changed the Activity and Activity A has gone to background.
Now, while the user is freely moving around the application, he just wants to come back to Activity A to check the download status,(which is something lets say I set some data to my TextView).
Now the question is, once the download is over , while my Activity A is still in background, my UI should be updated while Activity is still in background. This way the user feels he gets the data before he switches to Activity A.
Is this possible, if so how?
Summarizing my question, can I update the UI of an Activity while it is still in background with the Asynctask or Service which the Activity invoked to fetch data from server.
One post suggested that ideally I need to update the **UI in onResume(). My question is, is this the only approach?
once the download is over , while my Activity A is still in background, my UI should be updated while Activity is still in background. This way the user feels he gets the data before he switches to Activity A.
It isn't possible. You see, the Activity has to pass through the onCreate() and onResume() callbacks for the UI to be changed.
Also, you should be using a bound Service to update the Activity when it returns to the foreground.
onResume() would be the best approach. You may save the changes in a SharedPreferenes or Pass the data using Intent and show the changes before the UI is visible.
Another approach would be running a service and checking if the activity is visible. If its visible immediately update the UI or wait until user visits the activity. To check if the activity is currently visible see here,
How to check if activity is in foreground or in visible background?
In my app I need to call code whenever the app closes (pauses) and then run code again whenever the app starts again (resumes). For example, I call a web service that syncs the data whenever the app starts or exits. This is easy in iOS because of the central app resume and suspend methods.
I understand the OnPause and OnResume in the Activity, however, is there a central way to handle this? The user could leave the app on Activity3 and come back later, or be in another screen, etc. I'd hate to have to have the same code in every Activity's OnPause and OnResume to handle the "app" startup and shutdown code routines.
Any suggestions?
Thank you.
You could make a common Activity which just handles onResume() and onPause() in a certain way and then make every Activity extend from that one instead of Activity directly.
I would consider subclassing Activty, have each of your activities extend that and just do what you need to when any of them pause or resume.
You could use some static members in an Application class that your new activity base uses to track state or store whatever you need.
Also, Application class has an onCreate() method which will run each time the application is started. There is no pause or resume for Application, however.
Creating a main Activity that is extended by all your other activities and override the onPause and onResume.
But the problem you will face if you want to extend another Activity class such as a ListActivity.
Another approach is to create a new class that extends application and override its onCreate and create a static method that acts as onPause and manually call it by each of your activities