[Q] What is the proper way to close an activity/application? - Android Software Development

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.

Related

How to get Android to always open root Activity

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.

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

[Q] Screen on - New Activity created

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

BACK-Button to kill a task *searching*

Hi there,
wondering if there is a way to get the back-button finishing the app.
When i'm at contacts or writing an sms or surfing, i want to go back to desktop as fast as possible...but when i'm at contact/details, i have to press the back button 5-6 times to go back trough all the previous screen.
Is there a way/hack/app telling the back-button (have to be a hardware button) to end the tasks completly when holding it, for example?
THX THX
Press the home button? That will take you back to the desktop.
If you need to you can then use task manager to end the task.
Would be nice to see a hack/fix to add the back button to kill functionality though
IakobosJ said:
Press the home button? That will take you back to the desktop.
If you need to you can then use task manager to end the task.
Would be nice to see a hack/fix to add the back button to kill functionality though
Click to expand...
Click to collapse
I miss this from my oxygen rom'd desire
Pkplonker said:
I miss this from my oxygen rom'd desire
Click to expand...
Click to collapse
It's already in in MIUI and most probably will be also in CM7.
well the back button does kill apps.
if you start, say, the browser and press the home button, then enter task manager, the browser will still run. if you start the browser and press the back button and open the task manager, it will show that the browser is not running anymore (actually, the browser was a bad example, since after a little bit of browsing you'd have to push the back button quite a lot since its primary function is to go back). the same goes for many more apps, whatsapp, facebook, twitter, angry birds...
Pkplonker said:
I miss this from my oxygen rom'd desire
Click to expand...
Click to collapse
me too. adamG has apparently got a SGS2 though
Thinking about it, I would also like this functionality although it isn't so urgent for me. If more people show interest maybe I'll attempt it as my first android app For such a simple app my opinion is that it should be free and not packed with admob spam, but if another dev wants to attempt it that's their choice I guess.
My thoughts:
It should be quick to develop a simple app providing this functionality. Just have it bind to an event which fires when the back button is pressed long, and have it kill the app whichever is first in the foreground window Z-order (I'm fairly new to the Android API, so I use Win32 API terminology here for example only). The only complexity may be to make the event bound globally (in all apps), as opposed to only bound in the app itself - hopefully that doesn't require root! I guess it should be a background service which runs on boot, and perhaps a very simple gui app to control that service (in fact would there be anything to configure at all? What do you think?).
sl9 said:
Thinking about it, I would also like this functionality although it isn't so urgent for me. If more people show interest maybe I'll attempt it as my first android app For such a simple app my opinion is that it should be free and not packed with admob spam, but if another dev wants to attempt it that's their choice I guess.
My thoughts:
It should be quick to develop a simple app providing this functionality. Just have it bind to an event which fires when the back button is pressed long, and have it kill the app whichever is first in the foreground window Z-order (I'm fairly new to the Android API, so I use Win32 API terminology here for example only). The only complexity may be to make the event bound globally (in all apps), as opposed to only bound in the app itself - hopefully that doesn't require root! I guess it should be a background service which runs on boot, and perhaps a very simple gui app to control that service (in fact would there be anything to configure at all? What do you think?).
Click to expand...
Click to collapse
It would need to be in the framework of the rom, I think
Chef_Tony said:
well the back button does kill apps.
if you start, say, the browser and press the home button, then enter task manager, the browser will still run. if you start the browser and press the back button and open the task manager, it will show that the browser is not running anymore (actually, the browser was a bad example, since after a little bit of browsing you'd have to push the back button quite a lot since its primary function is to go back). the same goes for many more apps, whatsapp, facebook, twitter, angry birds...
Click to expand...
Click to collapse
Yeah it does, but you have to press and press and press...would be cool, if you can "hold down" the button for a sec and it will end the task.
Example...when i'm at handsentSMS > SMS > keyboard open...
Pressing BACK > close keyboard
Pressing BACK > closing SMS windows
Pressing BACK > closing handsentSMS
3 times pressing this button after every SMS suxx...sorry
Yeah i can press the HomeButton, but then its in my mind, that the app is running in background...isnt that bad at android-OS-structure, but its a bad feeling to me...
yes that's true. i was reading over your original post again and the first time through i must have overread the part where you named sms and the browser as examples. you are right, in that case, it does not work very well. an alternative way is to hold the home button, then press task manager and kill the app instantly. but that takes even longer. i guess your best option is to actually let it run in the background, if you want to get out of it fast.
pulser_g2 said:
It would need to be in the framework of the rom, I think
Click to expand...
Click to collapse
I suspected this may be the case, really hope it isn't but I understand the Android security model needs to enforce a certain level of isolation for each app in the VM I'll do some further reading of the documentation to find out for sure, since a simple non-root APK providing this functionality for users would be very welcome.
I'm too used to other less-secure API's (Win32) in which any app could basically do whatever it wanted to other apps and the OS; I guess thats why virus authors love Windows so much
yeah just for anyone not totally in the know, there is a difference in android between pressing the BACK button and HOME button to leave an app. using the back button to leave an app and go back to the home screen basically "kills" the app. it puts it in a state where it can readily be killed by the OS in an instant.
when just pressing HOME to leave an app, the app is technically backgrounded. the OS will keep it around longer or in a different fashion.
so backing out of apps with the back button is always the way to "exit" any app.

WP 8 and Multitasking

Hello there!
I would like to try it by myself, but unfortunately I cant. So, someone who tried the SDK, have you noticed changes in multitasking system?
Right now the only way to resume an app is using fast app switch. But I really dont like it. I rather just use the homescreen icon instead. Right now it relaunch the app.
Any changes on that? (oh please)
Thank you so much!
mikeeam said:
Hello there!
I would like to try it by myself, but unfortunately I cant. So, someone who tried the SDK, have you noticed changes in multitasking system?
Right now the only way to resume an app is using fast app switch. But I really dont like it. I rather just use the homescreen icon instead. Right now it relaunch the app.
Any changes on that? (oh please)
Thank you so much!
Click to expand...
Click to collapse
Windows Phone apps can never resume via the homescreen like iOS, due to the addition of the hardware OS back button.
To illustrate why; imagine you have an app that has start page and a settings menu. When a user goes to the settings menu, they can only go back to the start page by pressing the hardware back button (this is standard Metro design).
Now imagine a user opens the app, goes to the settings menu, then exists the app by pressing the Home button. They then do a few other tasks and then resume the app. They are now stuck in the settings menu and can't get back to the app start page; the back key will take them back to the WP8 Home screen (this is how the WP OS backstack works).
To get around this issue, Microsoft specify that starting the app from the front page always has to start a fresh instance, so the user can never get "stuck".
iOS has software back buttons on every page, so all apps can resume however you launch them. Android had the same problem with their back button (actually worse, as their backstack can be altered by the OS choosing to kill memory-intensive apps); to get around this, from ICS onwards Android apps are meant to have a software back button in the top-left, to go back within the application (hardware back key is still OS backstack).
Aphasaic2002 said:
Windows Phone apps can never resume via the homescreen like iOS, due to the addition of the hardware OS back button.
To illustrate why; imagine you have an app that has start page and a settings menu. When a user goes to the settings menu, they can only go back to the start page by pressing the hardware back button (this is standard Metro design).
Now imagine a user opens the app, goes to the settings menu, then exists the app by pressing the Home button. They then do a few other tasks and then resume the app. They are now stuck in the settings menu and can't get back to the app start page; the back key will take them back to the WP8 Home screen (this is how the WP OS backstack works).
To get around this issue, Microsoft specify that starting the app from the front page always has to start a fresh instance, so the user can never get "stuck".
iOS has software back buttons on every page, so all apps can resume however you launch them. Android had the same problem with their back button (actually worse, as their backstack can be altered by the OS choosing to kill memory-intensive apps); to get around this, from ICS onwards Android apps are meant to have a software back button in the top-left, to go back within the application (hardware back key is still OS backstack).
Click to expand...
Click to collapse
But it sucks so bad! They should review this. I hate to use the back button, and I hate to not resume the app. Using a common app, for example, WhatsApp. I was in a chat with someone. Then I hit Windows button and Im at start screen. Then I receive a message from the same person I just left the chat. What I do? I can open from the toast, can open from fast app switch (back button), or open from start screen icon.
If I open from toast, that will depend on what the app was meant to be. In WhatsApp it would take me to the chat, because of deep toast notification. But, right now, it needs to reload the whole app to open just the chat.
If I open from fast switch, it will resume the app right away. Nice. But in any other platform the message would be there waiting for you. Right now, in WP, it takes a lot to refresh the chat. You keep like 10 seconds staring at the screen waiting it. Its even faster to just reopen the whole app.
And if I open from start screen, its almost the same effect of toast, but it dont take me to the chat, but to the start screen of the app.
The point is, the fast switch is not helping that much. In fact, it would makes sense to change the fast switch to open when holding the Windows button instead of back button, and whenever an app is open, opening it from start screen icon just resume it. Actually, a lot of people doesnt even know, or even knowing, doesnt even use fast switch. Im not a common smartphone user, and even so I dont use fast switch.
For me, its the worse problem of platform. And I dont care about CE or NT if it works, but I care about it working at all. Doesnt make sense to put a whole computer in my pocket if it cant resume a single app.
i don't like the idea either to relaunch the app when you just have put it in background. then again, i also hope we will be able to close apps from the fast-appswitch-screen. and add an option to the gesture lovers out there: pinch out on homescreen to launch multitasking. or swipe from edge like w8. or anything like that. it would add to UI experience and would eliminate that 2-seconds-pause when pressing and holding down the backbutton.
Was the question not about Windows Phone 8?
Windows Phone 8 is supposed to behave differently, since true background processing is supposed to be enabled. I haven't played with the SDK yet, but I suspect that for non recompiled apps, things will behave as they do on Mango. But, I think that things changed to target WinRT and set to be able to run in the background will be able to resume right where you left off.
It wouldn't make sense for an app that is running and processing things in the background to restart when the tile is pressed.
It's been a while since I used Mango or wrote any apps for it. But, when an app is suspended, the dev has a specified amount of time to save the state.
That way when it is relaunched, the app can resume where it left off, by processing the saved state on launch. I thought with fast resume the app stayed in memory, but that was done through a registry hack and not directly made available by any carrier.
After doing some reading, the multi tasking enhancements might only add gps and voip to the currently supported background processing.
JVH3 said:
But it sucks so bad! They should review this. I hate to use the back button, and I hate to not resume the app. Using a common app, for example, WhatsApp. I was in a chat with someone. Then I hit Windows button and Im at start screen. Then I receive a message from the same person I just left the chat. What I do? I can open from the toast, can open from fast app switch (back button), or open from start screen icon.
If I open from toast, that will depend on what the app was meant to be. In WhatsApp it would take me to the chat, because of deep toast notification. But, right now, it needs to reload the whole app to open just the chat.
Click to expand...
Click to collapse
Tapping the toast to re-open the chat is the correct behavior here. I guess it's just bad coding that makes it take so long to resume; it should just be able to go straight to the conversation and skip all the "loading contacts...connecting" stuff.
JVH3 said:
Was the question not about Windows Phone 8?
Windows Phone 8 is supposed to behave differently, since true background processing is supposed to be enabled. I haven't played with the SDK yet, but I suspect that for non recompiled apps, things will behave as they do on Mango.
But, I think that things changed to target WinRT and set to be able to run in the background will be able to resume right where you left off.
Click to expand...
Click to collapse
Are you sure you're not thinking of Windows 8? For Windows Phone 8, no changes have been announced regarding multitasking or background tasks, *except* that a few select APIs (VOIP, location) will be able to run in the background, similar to iOS (not true backgrounding like Android)
Also we are talking about resuming, not background processing. In the WP8 SDK emulator, apps built into the OS don't resume; Therefore it's safe to assume 3rd party apps are not going to either.
JVH3 said:
It wouldn't make sense for an app that is running and processing things in the background to restart when the tile is pressed.
It's been a while since I used Mango or wrote any apps for it. But, when an app is suspended, the dev has a specified amount of time to save the state.
That way when it is relaunched, the app can resume where it left off, by processing the saved state on launch. I thought with fast resume the app stayed in memory, but that was done through a registry hack and not directly made available by any carrier.
Click to expand...
Click to collapse
When an app is closed the developer is meant to save the state, so that it can be reloaded if it is quick-resumed. However, once the app leaves the backstack (the 5 apps that appear in when you hold the back-button), this state is supposed to be discarded.
This is not a technical issue; it would be trivial for app developers to save the state and make their apps resume. The issue is that Microsoft's publishing guidelines (to get your app published on the WP app store) specifically says that an app launched from the home screen must launch showing it's introduction page, i.e. it can't resume. It could save some state, so a web-browser could still have all the recent tabs open, but it couldn't show the last one seen (ironically IE9 does resume it's state - guess Microsoft are allowed to break their own guidelines).
I agree it doesn't make sense to restart an app that is performing some background task; but then how to you avoid users getting stuck within a certain page, as in my example above? If WP8 includes a hardware back button, they can't change this policy.
Well, thats a shame. I hate reloading the app everytime I need it. Its so meaningless. I dont need VOIP, I dont need Skype running all the time. But I do need apps to be fast.
It really depends on how exactly the developers save their app state when the app is sent to background/tombstoned.
I, for one, use a text file to save data ( a lot of data) and proceed to loading the app as usual, and the moment the user presses a button, a pop up asks him weather he wants to restore or start anew.
I'm guessing that not every app will do this, as it is up to the developer to implement this.

Categories

Resources