LibGdx resume delay - java

The problem is when my game is hidden, there is a delay when it's resumed. Each time game is resumed it takes longer. This happens on any screen set including splash screen where assets are being loaded. There is no difference in delay when game is resumed to game screen or splash screen, or menu screen. That means there is no problem with a lot of assets being loaded. I tested other game called "Cut the Rope 2" and there is no such delay, game resumes immediately.
I would like to add, that when I launch application first time, there is no such delay it jumps straight away to splash screen. Also when I press back button, it restarts the whole application and there is no delay also. That means that there is only a delay when application is resumed and there is no delay at all when application is launched first time or restarted.
UPDATE:
My problem is exactly like the one posted in the link below. Problem wasn't solved there. http://www.badlogicgames.com/forum/viewtopic.php?f=11&t=11433&p=51515&hilit=resume+slow#p51515
So the problem is that this is called when my game resumes:
assetManager.finishLoading();
I didn't put that code in resume(). From the link above, I read that it's called automatically. When you press "Back Button" on tablet and then you resume the game, it takes time to launch application. If you press "Lock Button" and then you resume the game it launches game immediately but it freezes. So there are two different behaviors according to the button which was pressed. I would prefer to launch game immediately and not freeze but play my loading screen. Is there any way to do this?

The solution to this problem is to call
Texture.setAssetManager(manager);
In this way AssetManager.finishLoading() will not be called automatically when game is resumed. Therefore it will not reload any assets. AssetManager.update has to be called manually.

Related

Loading Images,Videos in background

I am trying to make an App which is going to display Some Images and Videos. So I am planning to add a splash screen of around 2seconds. After 2 seconds the user will be taken to the Main Screen of the App.
I want to start the loading of the Images, Video when then user is at the splash screen itself so that the user should wait for the least time when he is at the Main Screen.
So the loading will be started at the Splash Screen and then after two second the user will be taken to main screen irrespective of the completion of the loading.
Now since this involves two activities should I use a Async task or should I Use a service with an Async Task(For the callback of completion of code) within it?
Which one would be better. Also in Android 8.0 are there any restriction in using Services?
I think using a Async Task between two screens may cause Memory leak if not coded properly.
Any help would be really grateful.
EDIT: My app is having one more feature hence cannot make the user wait in the Splash Screen till the loading is over.
It is not very good to use AsyncTask for sharing results between 2 activities, because AsyncTack created in Splash activity will be destoyed (stopped) when switched to Main activity. Better to use service in this case and Main screen will subscribe for result.
So basically you want to start the download in the splash screen and continue the download in the activity that follows. In this way, you still have to implement a loading animation. In your case, I would recommend finishing your splash screen, as soon as everything is downloaded. In that way, you don't have to download anything anymore inside the app's lifecycle.
AsyncTasks continue to run even after switching to a new activity. You can try the following flow:
1. Splash screen
2. Trigger Async Task
3. Main Activity
4. Show Images/Videos
The only catch is, you will not be able to fix a time for #2 to complete to be able to start #4. This is the nature of AsyncTasks. You can workaround by using the OnPostExecute within AsyncTask.
Example: OnPostExecute call another method that will enable a button. Users can click on the button to view Images/Videos. But then, this might not be a good user experience to see some button suddenly getting enabled.
In that case I would rather create some Singleton with own Handler (that works in separate Thread), that will be started at Splash screen. After Main screen will start, it should ask that Singleton about respective data or should sign himself for receiving that data.

How can I synchronize animations on multiple views which does not start their respective animation at the same time?

I wrote a fade in fade out method for views. It works perfectly, but, as I understand, each view's animation is handled by a separate thread in the background, tackled by the android system.
That is a problem, because, I want to blink and the same pace.
Is it possible to do that? I tried to put my blinking method in a runnable and restart it every time a new View starts its animation, but it is not working because 1. you cannot simply restart a thread, 2. even if you can, the Animation will be handled by the system with different threads each running independently anyway.
How should I do this?
Let me narrate my goal again: I tap on a view, it starts blinking, fade in fade out style, using a method I wrote. Then I tap on another, both blinking at the same pace, with the first one perhaps noticeably changes its original blinking pace to be in sync with the 2nd one. Then I tap on another, another... no matter how many I enable blinking animation on, these views all fade in and fade out at the same pace.
Can you offer any advice? Either a specific one or a general one.

Android: Killing background activities

I have an application that contains many game screens, each is its own activity. Some of these game screens will use timers to initiate some functionality. The problem I am having is when the user backs out of one of these game screens, the timers are still running in the background. I know this because when I leave to the main menu the timer loads up the losing screen because the timer and activity were still running in the background process. I simply want to kill the activity if the user hits the phone back button (Not a button I have created but the devices back button).
This is the current method I use to try and accomplish this:
#Override
public void onBackPressed() {
super.onBackPressed();
this.finish();
}
It does not kill the activity.
EDIT:
I Should mention that stopping the timers does work but for some of my screens the win or lose conditions are done when a variable hits a certain number. For example, I have one screen that when an enemy escapes the view of the player a variable goes up by 1 when this hits 5 the player loses. This is not done on a timer, so because the activity is still running, the variable hits 5 and the losing screen is loaded when I am not on that game screen anymore.
Only the visible Activity will receive that finish() command. You need to start and stop your timers onResume and onPause
First of all, remove
super.onBackPressed();
And add
yourActivity.this.finish();
Let me know if that helps :)

Java Swing: focus JWebBrowser and press space when button is pressed

I have embedded a youtube video as shown here. I have several videos and when one finishes the next one starts automatically. I achieved this by looking up the video duration and starting a TimerTask.
Now there is one problem left: If the user pauses the youtube video, the timer continues and the next video starts after some time.
I thought of this solution but could not make it happen: Implement a JButton, that stops the timer, changes the focus to the youtube video and presses the space button. That should stop the video.
I played with the JWebBrowser.grabFocus() and alike but I was not succesful.
Any help would be appreciated.

App crashing after sleeping

I'm currently following some video tutorials on Android development, and it's gone fine up until the 8th tutorial, we make a splash screen, and after 2 seconds it's supposed to go to the main application. The problem is, after 2 seconds, the screen goes black and the app crashes.
Logcat Output
I followed the tutorial exactly as he did it, the only difference with mine is that I'm developing for 4.2 instead of 2.2.
I'm also using my Galaxy Note 2 instead of an emulator to test.
I did it all as he did, and pressed play, the app opened on my device and the splash screen was there for 2 seconds (i set it to 2000) and then the spinner (which I added) freezes. The screen goes black, and then it crashes and says "Unfortunately, The Basic Series has stopped."
I've tried both sleep(2000) and Thread.sleep(2000).
Code for the AndroidManifest.xml and the main.java are here
If anyone is curious, this is the video I was following.
avoid splash screens except if u really need them to do some preparation operations..
avoid Thread sleep otherwise you will face ANR crashes (use post runnable with delay instead)
avoid reading this hint: What is your activity class name? Menu, menu, MENU or uNeM?
not so good tutorial IMHO
not honorable mention: if u really want to do it in this (btw not recommended way) check video at 8:48 and try to add catch clause and log exception.
If you need to do some initializations , use AsyncTask , or a thread that upon completion will use Activity.runOnUiThread (so that it won't crash when doing UI operations).
If you need to just show the splash screen and close it after some time , use Handler.postDelayed .
In all possible solutions , don't forget to cancel them upon onPause/onDestroy (depending on what you do/need) , so that if the user has left the app (exited or left it in the background) , it won't open up with the new activity when he resumes it (or worse , show it while it has gone to the background) .

Categories

Resources