So I have two fragment alternatively replacing each other (both containing a countdowntimer), until the end of a game. I'd like to implement a pause button, who, like his name indicates, would pause the fragment underneath.
I was thinking to do that with a dialog fragment to do that smoothly but if you have another suggestion, I'm open minded.
When clicking on the button, a pause menu will appear and the countdowntimer underneath will stop. When resuming the game, it will continue where it stopped.
Thanks in advance !
As far as I know, CountDownTimer class does not provide a method for pause. The best workaround for this is to save the remaining time when the pause button is clicked, and to create a new CountDownTimer with that amount of time when the game is unpaused.
I've done this in an app I made some time ago, where I needed to manipulate the time by adding or subtracting the time, same idea. Make the new timer, cancel the old one, and start the new one.
Related
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.
In my android app, there is a SurfaceView. Whenever user touches it a short( 1 sec) animation is rendered on the SurfaceView by a AsyncTask. I face force close problem in two scenarios.
When used presses Back key while the animation is running.
In the arcade mode game duration is of 60 seconds. At the end of game, new activity is started. If at the end of the game , the animation is still running while the new activity is started, I get force close error message.
How can I check if the AsyncTask is complete i.e. the animation is over, before Back key press takes effect or new activity is started ? Using AsyncTask.getStatus() in a while loop is freezing the app. Or is there any alternative way to do this?
One of the way to wait for it to finish is like asyncTask.execute().get() ;
with this your main thread will wait this to finish
Actually i just want to know whether can we set timer when the activity changes.
Detail description:
startActivity(new Intent(this,Second.class));
I know that by using this code we can change one activity to another activity. By the above code when the activity is changing it changes quickly as know to everyone, but what i want is in button click event when i write this code, when i click that button the same activity needs to be on screen for some particular time (i.e., around 10 seconds) & after that it needs to change activity. I thought of keeping timer here but i didn't got any idea how to do that.
Can anyone please help me with this.
The easiest thing would be to simply create a Handler and post a message to it 10 seconds later.
Handler activityChanger = new Handler();
activityChanger.postDelayed(new Runnable(){
startActivity(new Intent(this,Second.class));
}, 10000);
Put that where you normally create your activity within the scope of startActivity. You're current activity should run for 10 more seconds and the new one starts up.
I've found out that pop up messages from my service provider(telecom network) is causing my app activity to get paused.Is there any way i can prevent these from pausing my activity?
I am not sure if there is much that you can do about blocking the pop ups from the service provider.
The easiest way to handle this situation though would be to shift your countdown timer or ringtone player to a background service.
What happens now is that when these pop ups occur your service is temporarily loses its focus and gets the focus back when the pop up is closed. Implementing your timer and stuff in the service will prevent this from happening.
EDIT:
best way is, write the entire layout as you want with all the buttons. in the onclick of activities you can call whatever the intents that you want. At the same time you can make the countdowntimer and ringtoneplayer as static objects in service.
So in the activity when you press the start button, you can just write service.countdowntimer.start() and it will start the cdt from the activity and the same for stopping the time and for the ringtone player too. You are just shifting your ringtone player and cdt to the background and implementing it in another class which extends service. rest almost everything remains same. you wont even need any sort of listener here.
I have an Activity that plays some audio from within a ListActivity. The user can also click on the list item and go to a more detailed view of that data. I want the audio to keep playing during that transition from one activity to another and keep playing once the detail activity is active BUT I also need to pause the audio if my activity is being paused or stopped for any other reason.
My issue is that I currently pause the audio in onPause() in the ListActivity because I want the audio to be paused when the user navigates away from my activity. E.g. when they press Home or Back. However, I don't want the audio to pause when my detailed view activity gets started. onPause() is called in both instances, so how can I distinguish between the two cases?
Shouldn't you be doing that in a service instead? Wouldn't it be easier? Then you can stop the service when one or another activity is pausing or exiting (depending on which one you want). You could also check if it's started, and stop accordingly whenever you want.
Or sorry if I didn't understand your question. I see you have a lot of points here on SO, so perhaps I'm just confused.
--- edited since your third comment:
A Intent to B: send Intent with name of music.
B onCreate: get name of music from Intent. Set flag x to true.
B onResume: start playing music if from A or resumes from last known position if resuming from B.
B back button: override and set x to false. Actually, you'd cover all points where your activity finishes.
B onPause: stop the music if(x), store last known position of music in memory and stop service.
Here I assume that you want music to keep playing even when returning to A (that's what you said, that's the problem), not just up to B. Setting x is important early on, IMO, because if any other activity (phone call, anything) appears, activity will stop playing the music immediately (user expects that) on onPause. According to Android guidelines, you know that you're getting back to A only if user presses back button or if you finish() your activity. You can fine tuning checking the position of activities in your task (don't remember how to do that right now).
Personally, I also wouldn't want to resume playback on A (say, B->call->home->A, applying step 3 to A, too), because a disruption in the logical flow of things happens there.
CAVEAT: I would make sure that there is no other way. I would try to see if you can 1) know in advance which activity is about to be displayed. 2) get any music service to hook up to your task (is that even possible?).
Anyway, just use my solution if you can't find a clever way to do that. That would be my suggestion. Good luck.