I am new to Android . As per the Android Developers Doc making an activity launchmode singleTop it will keep that activity intact . But its not working for me .I have an Activity where i have a countdown timer , what i want is when i leave that Activity on back press and return to that Activity that countdown timer should still be running . How to do it ? Please Help
I believe you misunderstood a bit.
Launching an activity in singleTop does not mean that the activity is "intact", it means that if the target task already has an existing instance of the activity at the top of its stack, that instance will receive the new intent - a new instance won't be created. (This is opposite to launching activities in standard mode, which every time there's a new intent a new instance of the class is created to respond to that intent.)
As others suggested, you could bind to a Service and update the countdown time from there.
"Creating a Service will always keep that timer running , instead i want it to keep running only when application is alive", that means you want to continue the timer from where the user left.
Store the timer value in SharedPreferences onStop() and retrieve the same onRestart() and then finally continue updating
Related
How i should implement a countdown timer if i need the following usage:
User starts activity A;
In activity A he starts the timer;
User leaves activity A, timer runs in background;
User comes back to activity A;
User see current (updated) values of the timer;
I tried to use a Countdown Timer but it fails at step 5 - after returning to ativity A i cant see values in TextView (in LogCat i do) and system starts another timer (this way, every activity re-open starts one more timer). Should I use service instead or ... show me the way, please.
You can't be sure that after you leave your activity it will be still alive. System could kill it at any time.
The solutions for this use case could be:
Start timer in foreground Service. Such types of services have better chances to be alive.
When you leave your activity you could save last timer value and last timestamp and restore timer with this info after activity is recreated.
PS. If you also want to fire the alarm when timer is finished you should use AlarmManager but keep in mind restrictions from android 6+ (M).
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 first activity of my application is a splash screen, where some animation is displayed while some loading occurs in a background thread using AsyncTask.
Once the loading in the background is done, I want to start a new activity. What is the correct way to do that ?
Start a new activity directly from the onPostExecute method of the AsyncTask class.
Check if the current activity is displayed before starting the new activity :
If the current activity is display, start the new activity.
If the current activity is NOT display, use a flag (boolean) and check the flag during the onResume method in order to start the new activity there.
My main concern is :
If my application went to the background (due to an incoming phone call, a home key press, ...) and the background thread (AsyncTask) finished executing, and a new activity is started from the onPostExecute method while my application is still in the background : What happens ?
Will the new activity start directly as soon as my application is visible again ?
I faced a similar situation: my application went to background and after some time the app started an intent displaying another activity. However, the app's behavior now depends on the os that it's running:
on pre 4.4 devices the app silently opens the new activity and remains in background. When the user resumes the application, he is prompted with the second activity already.
on 4.4 devices (tested on 4.4.2 and 4.4.4) the app opens the second activity, but after 3-4 seconds, the app pops to foreground, interrumping the user.
Hope it helps anybody. I'm still looking solutions for the second case in order to prevent the app from popping to foreground.
From my experience i am answering your question
Question1
If you using AsyncTask you have to start new activity in OnPostExecute(). In my experience this is the correct way of doing it.
Question2
When ever your press the home key or receiving phone call. Your activity will go in to background until you exit the app by pressing back button(at that time your app is exited). So when you come back to your app your new activity will be visible if background process get finished. Otherwise you will see the start splash screen.
I think that it is dependent upon the OS of android. It has defined some built in priority model for each of the components.
Look at the answer given by commonsware.
change process priority in android
this gives brief idea about components priority.
I have created an activity. I made the launch mode as singleTask in manifest file. I donot want multiple instance of the same activity should start. I am lunching this activity on button click of another activity.
If i click the button more than once then as i have made the activity as single task then If the activity already running then nothing happens. But I want to relaunch the activity without creating another instance. How to achieve this.
Thanks
Deepak
But I want to relaunch the activity without creating another instance
What you meant my relaunch without creating. I think you may have set of code that you have placed in your onCreate and you want to run when the activity again got control. If so its better for you to place that code to your onResume or onNewIntent. Because onCreate of the single task will only execute once
singleTask
The system creates the activity at the root of a new task and routes the intent to it. However, if an instance of the activity already exists, the system routes the intent to existing instance through a call to its onNewIntent() method, rather than creating a new one.
I want to develop a program which reminds word (english-turkish). The things what I have to do below and please correct me if I'm wrong or bad way I'm using.
Create and Main.java class as an activity which includes a textview to show english word
Create another activity which shows Preferences to setup interval time to remind new words
Write some code under the Save button click(inside the Prefs.java class) to save settings to SharedPreferences
Inflate menu inside Main activity to show Preferences Activity
Create a service with MyService name.
Get interval from SharedPreferences inside the OnCreate method of MyService.
Inside the OnStart method Run a Timer according to interval and continousley connect to web service to get a new word.
Periodically bring to front Main activity(don't want to create every time from the begining, just want to resume activity) and show new word.
When pressed New Word use the service's function to connect and retrieve new word and show in TextView in Main activity
When pressed Ok set Activity to Pause mode and show Home Screen
I have some difficulties to Resume Main activity and passing new word.
Do you know a way to bring front Main activity periodically while it is in resume state?
Try to make an intent in the onResume() call with this flag: FLAG_ACTIVITY_REORDER_TO_FRONT which causes the launched activity to be brought to the front..
For more information click here.
There is no difference between starting activity and bringing them to foreground - read about activity lifecycle.
Service is ineffective if you want to use it as simple timer. Much better approach is to use AlarmManager and scheduling next activity start. Here you have an example.
Then just override Activity.onStart() method, to fill all fields you want.