I have an app that responds to Internet message by creating a new Activity and user has 20 seconds to respond.
The problem is when the app is running on background.
I can show a notification, but when the user returns to the App the new Activity isn't started.
Is there any way to start an Activity even when the App isn't on foreground (without the activity getting focus) or any easy workaround where the activity would start right after returning to the app? (which would be worse solution, cause I would have to rework the sync timer :))
Thanks
Take a look at the Activity life cycle
Here you have the onResume() method you can override and do something before the Activity itself is shown. From there you could do some kind of check that you are returning from a notification or a check on that the user has to answer something now and launch a new Activity from the onResume() method.
Related
I want to execute a piece of code (say, for example, display a Toast) every time that the app is opened. So far I have managed to do this every time the app is launched by putting the code into my MyApp.java file that extends Application.
However, if I press the homescreen or back out of the app and then go into it, the message doesn't reappear. It only does when I relaunch the app. Any idea how to do this?
EDIT:
basically im asking how to execute code everytime the whole APP is brought to foreground (this can be first time open, after another app was used, after user backed out of app, etc). Where would I place onResume code? It wouldn't be in a particular activity, would it, since I want it to apply when entire app appears in foreground, not just particular activity.
You can try writing that code in your activity's #Override-d onResume() method.
The only way to do this is,
Determine which app is currently in the foreground
.Follow this discussion for getting an idea for the best way to do it.
[Determining the current foreground application from a background task or service
Suppose, if the function name is 'getCurrentForgroundApp()',
You need a service to execute getCurrentForgroundApp(); every one second
(1-second interval is depending on your purpose, can be lower or higher).
Now, you can identify which app is running foreground in every second.
So, check if your app is the one running foreground. If true, then execute the toast or code you need.
This is how app-locker apps showing lock screen over selected apps, whenever they come to the foreground.
You have to use the onResume callback:
Android API
Example of use from previous SO question
In activity class:
#Override
protected void onResume() {
super.onResume();
//your code here
}
I have an Splash Screen activity that launches an Asynctask that goes off and downloads and processess alot of data. It takes a while (20ish seconds) and it's a refresh of data so I dont wait for it and so send my users to a main activity where they can view the last cached data (with an indicator data is still downloading).
What is the best way (or any way) for the async task to notify the main activity (or any other current activity the user is viewing) that its done and to refresh screen and stop the indicator?
FYI, I dont want to just launch the AsyncTask from the Main as there are 3 other activitys that the user may have navigated to that all would need to know when it is done and to stop showing the indicator.
Your AsyncTask can broadcast an Intent when the data is available. The current Activity can set up a listener for that broadcast Intent and refresh the screen when it get it.
I am programming an app with the use of OpenGL and SurfaceView.
I need to detect when an activity finishes and I return to my activity.
I need it because I don't want to respond to touch and key events until my activity is shown.
Now I detect it with onResume, but onResume is called before the finishing activity disappears.
I need to enable these events when my activity is being shown (mostly because of onBackPressed).
Anyone with a solution?
EDIT:
find some information about it, so when the activity is doing something in its onStop/onDestroy method, then it is after onResume of my activity was called -> my problem
http://developer.android.com/guide/components/activities.html#CoordinatingActivities
EDIT2:
onStop and onDestroy are called when activity is not showing
EDIT3: found that it is caused because of new thread doing work in onPause, still no solution
Weird because onResume should be called when your activity is visible.
You could activate your touch controls with a small delay using a handler.
Over the past months I have been developing a versatile real-time game engine, and I have learned a lot, but I still feel very naive when it comes to the application life cycle. Specifically, I am trying to implement an Activity which can be shuffled in to the background by the user, then properly resumed.
My current architecture is as such: I have a an XML menu launcher activity which can create a real-time Game activity using intent. Relevant data in this Game activity is referenced through static data structures and variables. The Game activity creates worker threads in the onSurfaceCreate() callback of my SurfaceView object.
When a user presses the back button, the activity is destroyed, and they are sent back to the XML menu in the launcher activity. Fine, good for now. When the user presses the home button, the Activity is sent to the background. OK, great. My problems arise when the user tries to find their way back in the Game activity once is has been sent to the background. When the user touches the launcher icon, the Game is destroyed and the menu is re-launched. Also, when the user resumes the game through the task manager, the onSurfaceCreate() callback fires and the worker threads are started, but the game is frozen.
So, I have two questions. First, how do I resume my activity through the launcher icon, instead of re-launching the game? Second, when I resume my activity, what actions are necessary to restart my worker threads while persisting the game data?
Thanks!
EDIT: By request, I am including some code below. My onSurfaceCreate is a little bit complicated, it sends a message to another thread, which then implements the callback. I have verified that this implementation function fires when the Game activity is resumed.
protected void surfaceCreate()
{
Log.e(TAG, "surfaceCreate");
Thread gameThread = createGameThread();
gameThread.start();
}
protected final void onResume()
{
super.onResume();
resume();
}
protected final void onPause()
{
super.onPause();
pause();
}
These cryptic pause() and resume() methods simply set a boolean variable which prevents some game logic from being executed, they do nothing to hinder the worker threads, which should continue looping.
EDIT: Thanks Mohammad, for solving my first (although smaller) problem. It turns out that the launcher icon behaves differently when not connected by USB to the IDE. The second problem remains unresolved.
EDIT: All working! The second problem turned out to be an issue unique to my code, I apologize for that. I hope that this question can still be useful for those dealing with the launcher and IDEs.
There are multiple possible problems/solutions listed in this post. This includes misunderstanding of activity lifecycle and launchMode settings.
First, how do I resume my activity through the launcher icon, instead
of re-launching the game?
You are most likely missing definition for your onResume() and onPause() methods. Examples are here:
http://developer.android.com/training/basics/activity-lifecycle/pausing.html
Straight from the API:
onPause() Called as part of the activity lifecycle when an
activity is going into the background, but has not (yet) been killed.
onResume() Called after onRestoreInstanceState(Bundle),
onRestart(), or onPause(), for your activity to start interacting with
the user.
Check the Android Activity Lifecycle out:
http://developer.android.com/reference/android/app/Activity.html
One theory on your issue may be is that you're most likely going to onStop() (by hitting the home button) and the Android OS is looking for your onRestart(), but can't find it hence freezing/restarting.
Second, when I resume my activity, what actions are necessary to
restart my worker threads while persisting the game data?
Careful how you use the word restart. You want to pause and resume the application (not restart). Save data in onPause() (use a database or any other save feature you'd like). Load data in onResume(). Although, the activity should resume as normal if you just fill up these methods.
Now, if you want to save state for when you restart the application, you should save states in onStop() and/or onDestroy(). You should load states in onStart(). In order to save states, you can check this out:
https://stackoverflow.com/a/151940/2498729
For those using Eclipse (or any other IDE to run/test your application):
From what have you described you probably have overridden
android:launchMode in AndroidManifest.xml or if you are testing by
"run as" from Eclipse try exiting the application after installing and
auto-starting. Then start again from the emulator and test the Home
button behavior. I suppose this is because Android does not put
Activities on the OS stack when started from Eclipse and then the Home
button behavior is not as usual. If this does not solve your problem,
try reading
http://developer.android.com/guide/topics/fundamentals.html#lmodes.
I had launchMode set in my StartupActivity. THen I removed that (it
was set to "singleTask", it behaves like I want it; the app is
"resumed" to the Activity I expect, ie not StartupActivity but
MainActivity.
Source:
https://stackoverflow.com/a/1619461/2498729
http://developer.android.com/guide/topics/manifest/activity-element.html
According to this:
https://stackoverflow.com/a/3002890/2498729
You should change your andoird:launchMode to "Standard" (or "default").
Standard: A > B > HOME > B (what you want)
SingleTask: A > B > HOME > A (what you don't want)
I have a little problem with an alert View, the idea is that when the app starts, it asks on background for new data for the app, so after receiving the information an AlertView should be shown if there is new data. The problem comes because the AlertView is only shown on the main activity where I launched the asyncTask, so if I'm already in other activity, the alert will appear when I come back to the main activity and the Idea is that the AlertView is displayed in whatever activity the user is in.
My first thought was that the Context that I send to the AsyncTask is the problem so I've tried using getApplicationContext() so the app crashes in the moment of creating the alertView. So I'm, looking for a way to display this AlertView or something equivalent on the screen wherever he is on the app, does any one have ideas?
any idea would be greatly appreciated
you can use broadcastreceiver in every activity which will contineous monitor your background service contineous whenever event occoured using broadcastreceiver display alertbox with content received by receiver.