[Q] Screen on - New Activity created - Android Software Development

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

Related

[Q] What is the proper way to close an activity/application?

I started to develope an application.
When I wanted to close the application I clicked on the home screen,
but the app is still playing in the background.
Than I tried to click on the "back" key, but nothing happend, so I've
created a key event listener for the 'back" key, and inside that
method I wrote finish();
Now my app is closing but I think it's still running in the
background, because when I go to "manage applications" I can still see
it in the "running" tab, and if I press it, I see I can click on the
"force close" to actually close the app.
What do I need to do in order to really close the app without leaving
it open (one way or another)?
Thanks, Yaniv
You does not need a Exit Button, because Android will close things up as needed.
Perhaps you should read this: click
Otherwise you can exit your app with System.exit(0) at the end of your
onDestroy() methode.
Yes, I do know that Android is closing unused apps, but I do want to put also an exit option.
And thanks, the System.exit(0) worked for me
How does that compare to having an Activity-extending class call finish() from the main UI thread?
Actually... now that I think about it... if you're going to call it, do you have to call finish() as the last thing you do before returning from onResume(), etc? Or is it literally a black void from which the calling thread is guaranteed to never return? If you look at the Javadocs, they merely say that calling it indicates that the Activity should be closed, with the result propagated back to the parent (if any).
I've gotten burned by Java enough times over the past 10 years to know it's dangerous to read more into Javadocs than they literally say, regardless of how commonsense and reasonable it might seem. Going by the Javadocs' literal wording, it would be perfectly reasonable for a call to finish() to simply mark the Activity as eligible for aggressive closure and recovery... then return from the call and have a trainwreck after it executes code in the following finally{} block, or between the point where you called finish() and the return from the end of the current method. It would be outrageous, make lots of developers mad, and cause huge problems... but technically not inconceivable... which is why traditionally, I end up following every call to finish() with throw new RuntimeException("WTF?!?");, just to be safe and guarantee that execution will never, ever continue beyond that point.

[Q] Task not resuming to last activity when launched from home screen

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.

Activity Popping LOL

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

Android Wear APP Showing White Screen Above Activity at Startup and Resume

Android wear: white screen above activity at start up + when resume
Hello,
I'm having a strange behaviour with my android wear app. My app structure is basically: a loading activity that starts another activity which contains 4 fragments and the user can slide between those fragments.
The strange behaviour happens randomly but with the same pattern: a white screen appears "above" my activity/fragment and I have to dismiss it (android wear style: swiping on the left) to access my activity/fragment. This happens either on first start up of the app or after I clicked on the physical button of the watch and resume the app by clicking back on its icon in the app's list of the wear.
What I tried already is the following:
I put breakpoints on the onCreate of Activity and Fragment, but the debugger does not stop on it when this white screen appears. I concluded that this white screen is neither an activity nor a fragment.
I also removed all the "pop up" elements such as Toast and so, which I first thought were the root cause of my problem.
I also tried to follow the different advices of "how to remove white screen at start up" (such as putting in the manifest "@android:style/Theme.Translucent.NoTitleBar.Fullscreen", setting the "android:windowBackground" to another color and so) but it didn't resolve my problem.
I also looked up for post on stack overflow (the closest I found is this one: Pressing home button on Android - white screen when resuming app) but I didn't find any solution yet.
My problem happens both on emulator and real wear device (I'm using a moto 360).
Has somebody else experienced the same problem? Anybody has an idea where I could look further?
Thank you a lot in advance for your help, I'm really stuck and don't know what else to do.
Best,
Jenny

Question: Remember CM7 RenderFX? I want to generate an intent to turn it on.

My goal:
RenderFX is a CM7 settings feature that puts a color mask of your choosing over the screen. I believe it is generated by an app called CMParts.apk. I am running on the Neutrino 3.0.2 ROM for the Atrix 4g. I love this ROM paired with this phone. It's very minimalist, but does everything I could possibly want.
There is a widget built into CM7 that generates the intent that I want, but it's hideous and inconvenient (I have a picture of it attached to this post). I'd like to be able to trigger it maybe from a soft key long press or through an invisible widget in which I can touch the home screen to activate it. I have the means to create the soft key and/or invisible widget. All I need to do is figure out how to find the intent and write it in such a way that one of my apps (Macrodroid and/or DesktopVisualizeR) can use it as a trigger to turn on RenderFX.
My questions:
Would anyone here know of an app or file in the android system that shows the intents being generated? From here, I would be able to get clues as to what app the RenderFX runs through and the language that calls up the service.
Also, what is the proper format to request a specific intent from an app on android 2.3.6 (CM7)? I have no idea how to do this.
Maybe, someone can shed some light on this.
Thank you so much.
Crazy filter, idk though

Categories

Resources