In my android game there is an arcade mode which runs for 60 sec. The gamescreen consists of a gameboard, which consists of a 6x6 matrix of coloured circles drawn on a surfaceview. There is a timer and scoreboard to keep track of time and score. Timer is basically a separate thread sleeping for 60 sec and updating a handler attached to UI thread every sec. As soon as time left becomes zero the games goes to another activity where the player's current score and past scores are displayed. If the player presses back key then previous activity (gamescreen) becomes visible, however the scoreboard is not reset but the matrix is redrawn. All the coding is in onCreate() method or new methods created for the game. There is no code in onPause() or onResume() methods. Then why the surfaceview is recreated and redrawn ? I dont think pressing back key runs onCreate() method.
Your first activity will be destroyed if you call explicitly call finish().
Even if you do not, your activity can be destroyed any time after it becomes fully obscured.
So it is wrong to presume onCreate() will not be called.
Related
In my game when I press menu key and then back to the game it continues from where it paused
e.g. if I have a sprite moving to 0, 0 from full screen width and height and stopped at width/2 and height/2 it will start from this point and so on other things like music which stops then continues although I do nothing in pause and resume
My question is what should I do inside pause() and resume() of screens or main game class or should I leave them empty as they are ?
In main game class pause() and resume() I call super.pause() and super.resume()
I am loading my assets in show() should I do thing with this ?
When you press the home button, your game is not rendering until you open the game again. So your sprite moves on (is beeing rendered) at the same position because the reder() method was not beeing called while you left the game, thus not changing the coordinates of your sprite(s).
The pause() method is called on Android when the Home button is pressed or an incoming call is received. You could leave this method empty or for example save your game status to file.
The resume() method is called when the application resumed from pause state, that means if you open it again. You could also leave this method empty or for example show an options menu, so that the user knows that the game paused while he went out of the game.
For further information on the life cycle and states of a libgdx application, check the official documentation for this.
You could load your assets at the application start up (maybe you should use an AssetManager) in the main class (which extends Game) and pass a reference of the assets/AssetManager from the main class to every new Screen class you instantiate. This link in the libgdx wiki explains the act of managing assets.
I have a timer running in my activity. Let's say user switches to another activity. Should i pause my timer in that case' because sometimes my whole activity gets killed when i resume my application?
Hold your timer remaining time/value/etc into a global static object. When you switch back, use it for your timer or design a good application lifecycle with onPause, onResume etc.
My android application has 2 activities:
In the first one (MainActivity), the user chooses some parameters and these parameters are sent to the second activity (Display).
The second activity calls a web service, and according to the chosen parameters, the web service returns a value. I use the returned value to draw a bar chart of the evolution of this value. That's why I created a timer in the second activity that I put in the onCreate() function:
Timer t = new Timer();
t.scheduleAtFixedRate(new TimerTask() {
public void run() {
finish();
startActivity(getIntent());
}
},10000, 10000);
So every 10 seconds, the second activity is called again and the bar chart is updated with the new returned value.
The problem is, after the 2nd or the 3rd execution of the timer, several identical values are returned at the same time, as if the activity was called several times. And then the application starts freezing (but doesn't close).
I'm using the charts provided by this library: http://android-graphview.org/
I've also tried using the functions provided on the above website (resetData and appendData), and also the invalidate() function, but nothing works.
Any ideas why this happens? Is my way of refreshing the activity wrong?
The way you described it:
you create a new Timer (see 2.) with every call of the second activity
The purpose of each Timer is to call the second activity (see 1.) every 10 seconds
As a result the amount of times the second activity is called as a function of time increases exponentially.
A possible solution would be to move the timer to the onCreate method of your Main Activity (and still call the second activity from it).
This way there should be exactly one Timer active at any time.
EDIT:
As commented by Marius, an Activity might not be the optimal choice. If there is no user-input and the only thing the activity does is call a webservice and return the result, a method called from your Main Activity would be sufficient.
Firstly, calling finish() and starting the activity, NOT A GOOD IDEA.
Secondly, As far as i have understood your scenario, calling an AsyncTask inside a Timer after every 10 seconds is a better way to accomplish this. Call your web service in doInBackground() and then update your UI from onPostExecute(), this way you can avoid calling finish() and relaunching your activity every 10 seconds.
Finally you are creating a new Timer instance eveytime your Activity is called, so creating a huge number of Timer instances hanging your application.
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
I am calculating creating apps for Online Shopping in which i am having a Deals Start Date and End Date..
From this two date i want to calculate the time remaining for the Deal to end in Seconds basis.. I calculated remaining days,seconds,hours,minutes..
I want to display the Remaining time in Days:Hours:Minutes:Seconds format...
Although i move to next activity this time should runs in background in which the second must updated every seconds..
If i move this activity again i want to display the Updated Remaining time...
Also in my UI the Remaining time must get reduced second by second.
Instead of building a service I would create a Handler that updates your Userinterface.
You can now call sendEmptyMessageDelayed with a delay of 1000ms. In the handleMessage method you can update your UI and call sendEmptyMessageDelayed again.
Don't count on Android to call you exactly in time. recalculate the remaining time every now and then instead of just decreasing it by one.
If the activity is in the background you shouldn't update the UI because the activity is paused. Just disable the whole updating process in the onPause method and reenable it in your onResume method. If you method is destroyed from the system while it was in the background your onCreate method will be called again and you have to recalculate the actual remaining time.
Make sure you understand the ActivityLifecycle Process before implementing this changes.
Sounds like you need a Service and an Activity with the Service performing the timing operation and the Activity presenting the time information.