Continue timer even back button or another activity is opened - android

I'm trying to make a CountDownTimer App with some influence from the Android documentation. The App has a button and textview. When the button is clicked the countdown starts and when the same button is clicked the countdown timer stops. The timer is getting stopped when I press the back button or navigate to another activity in the App.
I want the timer to continue counting down even when the above operations are performed and the activity should be destroyed only when the timer isn't running.
How can I do this? Can anyone give me an in detailed answer?

Whenever you navigate to next activity your current one goes to onPause()(if not finished()), and the UI thread goes to sleep, so in order to update your timer when you leave and again come back to the activity is updating the UI whenever your Activity resumes itself(which is what onResume() is for). Trying using a boolean to detect pause event this is because when the activity starts it calls onResume also.
So summary is whenever leaving current activity store a boolean and while returning check it in onResume() and update Ui accordingly. Hope this helps.

Related

Android - How To Override the "Back" button so it doesn't stop() my background Activity?

Android - How To Override the "Back" button so it doesn't stop() my background Activity? For example if my Quiz app run the countdown timer. If user click on back key the count down timer are in running . I want Back key not effect on timer stop or pause
I think a better solution would be to move the timer to a separate service that runs independently of the Activity you are referring to.
Or you could save the state of the activity once the back button is pressed, and restore onCreate/onStart.

Android - Resuming already running activity

I have an android application which has only one activity having only one button. When we press this button, another thread starts which does some particular work. If the button is again pressed, then the running thread stops.
Now the problem is, if i leave the thread running and go to the home screen and again press the launcher icon of the app, it starts a new activity. Now if i click on the button of the activity, another thread starts. Now I have two threads running at the same time, which i don't want.
I want to reopen the previous running activity and then when I click on the button again, the already running should stop.
I tried using launchmode: singleTask, singleInstance but nothing seems to be working.
Please help how to handle this problem.
Try reading the docs # http://developer.android.com/guide/topics/manifest/activity-element.html
--
"singleTop"
Conditionally If an instance of the activity already exists at the top
of the target task, the system routes the intent to that instance
through a call to its onNewIntent() method, rather than creating a new
instance of the activity.
Alternatively
you can control the thread based on the onPause()/onCreate()/onResme(),
pause in the onPause()
create/start in the onCreate()
restart the thread in the onResume()

Button got stuck

I am creating an app which has a button which performs some functions on the data.
It is working well for some data but for some data when the button is pressed the button is getting stuck as in the button remains in the pressed state.
What are the reasons for a button remain in pressed state?
The only time that the button resumes from a pressed state is when all the code on the listener is run. You are most likely doing calculation intensive work in the button (in the main thread). If this is so if you click anywhere else in the application you should see an ANR (application not responding) message that closes the application.
Another reason would be that you switch the state manually and forget to cancel it on your on touch listener (if you have one)
http://www.youtube.com/watch?v=UApv-ZMJ51g

Suddenly stopped hitting onCreate

I have a home screen widget with a button with a pendingIntent. Click it, it bundles a boolean to indicate where it came from and it brings up my main activity.
In my main activity in onCreate, I check for a bundle, if the boolean that says it was launched from my widget is true, I perform an action.
Every time I hit my widget button, it should launch my main activity, and run through onCreate.
And it does, on the emulator. And it did, on my n1. Then suddenly it stopped on my n1. I can still get it to hit onCreate when I launch from the widget but I have force stop or reinstall and then it only does it the first time.
What is going on with this?
What's going on is the Activity lifecycle. Android will keep the same instance of your Activity around so that the user can return to it later. Monitor some of the other lifecycle methods like onStart and onResume to see this in action.
You may see a difference in behavior if the user hits the back button vs. the home button. The back button will finish the current Activity by default, whereas home will leave it alone unless Android decides to kill it for resources later.

How to handle that the application is minimized by HOME button

An issue has appeared a few days ago.
I have an application that listen for GPS location. The listener is a background service that works all the time, this service saves data in application level and each activity reads this data. So, when i press back button i am able to catch this event and i can stop the service, but when i press HOME button the service is still working although the application is in background mode and this consumes battery, because the GPS always works. How can i handle this event? I do want to stop all services when the user presses HOME button and start them again when user gets back.
10x
It sounds like you're catching the back button either via an onKey... method or in onStop. You should place your code in the onPause() method to ensure it's used whenever the app gets backgrounded.
You can not handle home button events in your Android application. Google has made it for internal use only.
LINK 1
LINK 2
#Override
protected void onUserLeaveHint()
{
// When user presses home page
Log.v(TAG, "Home Button Pressed");
super.onUserLeaveHint();
}
You could create a OnKeyListener, and check if the pressed key was Home. I never tried it, though.
create a custom activity called customActivity extends Activity now override method(in customActivity) to catch home button event and stop your service(create & start service in application class). Now extends customActivity instead of Activity for any activity_class.
Long press the HOME button, it will enlist the running process, select the one you want and then exit it gracefully.
OR
From Home -> settings -> application -> manage application
you can kill the unwanted process.

Categories

Resources