so Im making a game that has different classes. The 3 ones in this problem are "Menu", "game", "update". "menu" is the first activity that gets run and is used to launch "game", and game calls "update" (which contains the timer). the timer works perfectly on the first launching of "game" (which is called as an intent from "menu"), but if I go back to menu by using the 'finish()' method in game, and then relaunch "game" from menu, then the timer goes 2-3 seconds faster than it should. Any ideas on why this is???
Related
I have a program called Fred (names have been changed to protect the innocent) which I created in embedded Visual C++ 3.0 targetted for the Pocket PC 2002 platform.
The main dialog is a blank dialog - that is, it contains no controls.
When Fred starts, it shows this dialog for a split second, then executes what I call a Dialog Driver. This is simply some code that decides in what order to show other dialogs.
This driver shows the first dialog proper - the entry screen for the application with various option buttons. Depending on what button the user presses, they are taken through a series of dialogs, entering information as they go.
It is the dialog driver's responsibility to show the correct dialog, let it dismiss itself, then display the next one, so that there are at most two dialogs in existence at one time (the blank dialog and the one the user sees). This is opposed to the other traditional method of making each dialog itself display the next dialog in the sequence, with potentially tens of dialogs all laid on top of each other.
Here is the thing: on the XDA I, Fred works seamlessly. You always get a split-second pause between dismissing one dialog and showing the next. In this split second, you see the underlying blank dialog and the user thinks nothing of it.
On the XDA II however, the pause still occurs between one dialog and the next, but this time it sometimes lasts significantly longer. We're talking several seconds of seeing the blank dialog and there is no pattern to when this occurs.
On one occasion, we spent 4 minutes staring at the blank dialog, waiting for Fred to display the next dialog and in the end decided to interrupt it by tapping the Start button. This magically displayed the next dialog's contents with the Start menu on top, making everything alright again.
Right, I've rambled on for long enough. You probably stopped reading hours ago. If anyone has experienced similar problems or have any idea why this might be happening, I would be very grateful to hear from you.
Specs:
Fred is an embedded Visual C++ 3.0 program targetted for Pocket PC 2002, running on an O2 XDA II running Windows Mobile 2003.
Ok, so I have been struggling for a couple days with this, and I hope someone can help me.
I want Android to always run my splash screen activity, because I load a bunch of data from web services that I need throughout the rest of the app. This works fine with the flag android:clearTaskOnLaunch="true" set when you launch the app from the home screen.
Where it does not work so well is the long press of the home button and then selecting the app. In that case it starts the app in whatever random spot the user left it last. The problem is that if the app has been killed due to memory, it still shows in that recent apps list and when a user restarts the activity from the long press, all my data is gone so the app force closes.
So, my ugly workaround is to remove the app from the recently run list. I would much rather it showed up there but that it started my app the same way as if it was clicked on the home screen.
Anybody know how to do this? The ebay app pretty much does this, so I know it can be done.
-frank
Instead of reading your data in OnCreate, you could do it in OnResume. See the flow chart here:
http://developer.android.com/reference/android/app/Activity.html
as you can see, OnResume is always called whether the app is created initially, resumed from pause, or reloaded.
kaediil said:
Ok, so I have been struggling for a couple days with this, and I hope someone can help me.
I want Android to always run my splash screen activity, because I load a bunch of data from web services that I need throughout the rest of the app. This works fine with the flag android:clearTaskOnLaunch="true" set when you launch the app from the home screen.
Where it does not work so well is the long press of the home button and then selecting the app. In that case it starts the app in whatever random spot the user left it last. The problem is that if the app has been killed due to memory, it still shows in that recent apps list and when a user restarts the activity from the long press, all my data is gone so the app force closes.
So, my ugly workaround is to remove the app from the recently run list. I would much rather it showed up there but that it started my app the same way as if it was clicked on the home screen.
Anybody know how to do this? The ebay app pretty much does this, so I know it can be done.
-frank
Click to expand...
Click to collapse
In the onResume for the activity that is crashing, you could test your data to see if it's still there, if it's null you can run the code to repopulate it again.
My android application has a login screen that is launched as the main activity with intents as follows:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
Nothing else is out of the ordinary (that I am aware of, concerning the other activities, etc, no additional intents set on those or anything).
With the emulator, if I login and the second activity is launched, then I press the home button, if I launch the app again from a homescreen shortcut the task returns to the second activity. This is the desired behavior, as I don't want the user to have to login over and over again.
With both an HTC Incredible and a Droid X (only phones I have access to), if I login and the second activity launches, then press the home button, if I relaunch the app from a homescreen shortcut, it always launches the login activity, which is not the desired result. I want it to resume at the second activity where I left off.
I do have a task killer on the two phones I tested with, but they are set to not automatically kill any tasks and I don't believe this is happening because when I launch the app again for the second time and it shows the login screen, if I click "back" from there, it goes back to the activity I was hoping it would resume to, so the activity stack seems intact.
Does anyone have any insight as to why this works as intended with the emulator but not on the phones? Thank you very much.
I posted this on stackoverflow as well, but since I am new to this forum I could not include the link.
Note
I also just discovered that if I resume the app by holding the home screen button down and choosing the app from the recently used list that pops up, that it resumes to the last activity I was on properly. So it seems to just be from the app launcher shortcut or a home screen shortcut.
You can override that by setting android:alwaysRetainTaskState to True in the root activity.
http://developer.android.com/intl/fr/guide/topics/manifest/activity-element.html#always
This attribute must be on the root activity, so if your root activity is only the login activity and not the primary activity you want the user to return to, then you will have to do something a bit more complex. You will either need to make the login forum be a view on the primary activity, or make the login activity store the information and re-launch primary activity (not recommended).
You might also want to look at android:launchMode="SingleTask"
The above will solve the issue you are having, but the best solution by far is to instead use a long running service to handle the connection and login state, and make your activities just use and configure the service (login creates and sets up the service, primary activity acts as a proxy to the service commands, and logout shuts down the service). This will also prevent the login from being lost if memory gets low enough to shut down an activity, but not low enough to require a service shutdown.
I was able to get it working, thank you RoboPhred. The funny thing though, is nothing extra was required.
I think what was happening was that the old shortcut I had on my home screen was just that, an old shortcut. It was most likely botching something up. Once I uninstalled the app and created a new icon on the home screen, all is working fine.
Most likely this is why it was fine in the emulator as it fully installs the new apk.
So other devs be aware that old shortcuts may have some iffy intents depending on old builds etc and when in doubt fully uninstall the app and create new shortcuts.
I just wanted to thank you once again for at least replying with a potential fix, as it got me testing it again.
Thank you very much.
First screen is a Main Menu Activity.
It calls an activity (a quiz) based on user choice. User can abort to Main Menu with a button.
Quiz runs and at end, button is hit to score which opens new activity for scoring.
So, I have
Activity1 (Main Menu)
Activity2 (Quiz)
Activity3 (Scoring)
Now, it's easy enough to go back to the Quiz from Scoring with a finish() (and I do have that working and disabling all the quiz answers). That "pops" Activity3 off. And Activity2 can go to the Main Menu on button press (a finish()) and that pops off Activity2. All is good
Now, what if I want to go to the Main Menu from the Scoring Activity? I don't want to call a new Intent on it do I? Won't that leave Activity2 and Activity3 "un-popped"?
You know, when you leave something for a couple of days, it always seems to resolve its self LOL.
So remember coders, if you get really STUCK on something, let it go for a couple of days...it'll come to you
Hi!
I've found a strange Activity behavior while programming a game for Android (currently with API level 8)
Following Situation:
The Main Activity only consinsts of one View on which the Game Thread draws all the sprites and stuff. In the manifest I checked android:multiprocess="false" and android:launchMode="singleInstance" for the Main Activity, so that Android only creates one instance of this activity, so you always come back to your left game after pressing the home button and starting the app again. This works great, but if I turn off the screen WHILE in game, and the activity is active, and turning it back on, Android creates another instance of the game (the Main Activity), so it starts over again.
So my question is, what could I do to prevent this and have the same behavior as if switched back to Home Screen before turning off the screen?
(The testing device is a HTC Desire HD with Android Revolution HD 5.1.7, if this is necessary information for helpers )
I hope someone here can help me!
I think you will have to listen for the screen off intent and handle this situation very explicitly. Maybe make the pause screen come up and then when it turns back on have your activity check for instances of itself and then deal with it.
Do you use static activities?
From something awesome
Thanks for your answer
The ScreenOff/On Intents method would be easy, but how can I recover the old Activity in the new one?
The Activity should be static, cause of the manifest properties, or not?
Another idea: In the onPause() Method queck if it was Screen off (with the corresponding intent) and 'call' the home button
Is this possible? And how could I realize this?
There is a way to use adb to send the home key press. And you can run adb on the phone with getRuntime().exec("adb keyevent xx")
Where xx is the home button constant
From something awesome
Thanks for your help!
But I made it now a much easier way...
One single 'moveTaskToBack()' in the onPause and everything works perfect!
Nice. Its always best to assume that android will do nothing to manage your app and to just do everything very explicitly
From something awesome