Reg back button behavior in android - android

I am having a specific problem in my android app. I have 2 activities and the flow is from
Activity 1 to Activity 2.
I press back button from Activity 2 and then it goes to activity 1. This is great. But in some devices if I stay too long on activity 2(Like for days, I check it once in a while ) , and when I press back button it doesn't go back. So I guess history is cleared by android os for getting more memory.
I know that I can manually override onBackPressed() and achieve the functionality, but I need a perfect solution for this. so it will be like
If the history is all right let android handle back, If history is disturbed then I should be able to handle it.
IS there any way to check any issue with history?

Related

How to I delete a previous SavedInstanceState?

I'm making an Android app but I keep running into a odd bug. After the game, if you tap the 'Back' button on your phone, you end up back at the finished game and you can keep playing!!
How do I prevent this from happening? Is there a way to delete the previous SavedInstanceState? If so how?
If you really don't want to restore the data don't pass them to your super class. However I would prefer to reset the game data myself.
I guess the full solution is to call finish();. I guess you have this activity stack: Menu -> Game -> End summary. So when you start your "game over" activity add a finish() call to close your current game. So a back button press let the user go back to your menu.

Is Home Activity instantiated multiple times?

The official Dev Guide of Tasks and Back Stack says, activities can be instantiated multiple times, and Home Activity is taken as an example
So I tried it out as the graph illustrates:
Launch Activity 2
Press Home button
Launch Activity 1
Press Back button (so I return to Home screen)
Press Back button again
But I did not go back to Activity 1. Thus, it seems that Home Activity has not been instantiated multiple times. Is it so? If so, how is it kept in a Back Stack?
EDIT: Sorry, I should've clarified earlier that I didn't write any codes to test it. All I've done is just launching applications on favorites tray. I'd better go to read the source code and search for the behavior of Home Activity.
Anyway, I don't think Home Activity is a good example here to illustrate multiple instances.
Your issue might be that you might have called finish() in your Activity2. Or, the OS clears up the 2nd Activity before you return back to it. The behavior you are trying to attain on your own has no guarantees. You can't force an Activity to keep running so that you can return back to it.

android - return to app from service or notification

i'm having some problems with the flow of an app i'm working on.
basically , i have a service that always holds a notification , pressing on the notification should return to the app's most recent activity , without re-opening it (meaning that it will resume).
also , on a specific activity (and maybe others ) , i need that clicking on the back button would exit the app (and the service) , so the next time the end user starts it via the launcher or via long pressing the home button , it will go back to the first activity .
in short , the requirements are:
service notification click -> resume current activity , no creation of new activity.
back click on a specific activity -> close app entirely (clearing all app's stacks).
so , for example , if i have activity A which calls activity B (which is the special activity) :
if the end user has clicked home , and then returned to the app via the notification (or launched via launcher/long press on home button) , it will return to the exact state of activity B that he left it .
also , if the end user has pressed the back button on activity B , the app is closed (and the service and notifications shall be gone) the next time he opens the app (no matter how) , he will go back to activity A .
i've tried to use "singleInstance" on activity B , but then it will always get back to activity B , since it is inside its own task , no matter which flags i use(i have tried FLAG_ACTIVITY_REORDER_TO_FRONT and some other flags) .
without using it , the notification will open a new instance of activity B .
can anyone please help ?
an alternative way would be to set the notification's intent to start a new , fake activity , that will close as soon as it is created.
the intent will also have the "FLAG_ACTIVITY_NEW_TASK" flag .
hopefully this method will work for everyone .
too bad this solution seem more like a workaround than a real solution.
another alternative would be this link:
Change notification intent in Android
jelly bean (android 4.1) now introduces a new API for this exact problem :
http://www.youtube.com/watch?feature=player_embedded&v=Yc8YrVc47TI#t=830s
however , i'm not sure i understand how to use it and how it works. is it possible that it re-creates the entire stack of the activities ? isn't it quite problematic since they might include data that wasn't there before (since they are refreshed) ?
it also sounds problematic since it means that i need to monitor all of the actions in order to restore them back later.
ok , even though it's not exactly the answer , for my case , i've used "singleTop" for activity B , and chose to close activity A when moving to activity B .

Android Back-Button-Cycle Issue

I'm ware of androids life cycle and that it's not needed to add a "exit" button in the application.
But still, this back button stuff isn't really working out well.
You maybe know this issue from the default SMS-App that android comes with: you open it when you get a new message and exit it using the menu button or something else.
After like 20 times doing this, you then decide to exit the app using the back button, what happens? you have to go back though 20 views. every time you press back you return to the "all messages (by sender)" listview and when you press back there again you return to the message opend 20-1 (message 19). again you press back and return to the listview and again you press back and end up at message 18. till, after40 times pressing back, you finally exit the messanger app.
same happens when for example you got a action bar with a "home" icon which opens the main screen of your app. the user picks a action and the new activity starts. than the user clicks the home button and returns to the main screen. when pressing the back button - no matter if you call finish() in the onButtonBack listener or not, you the user would expect the app to exit, but in fact the app returns to the previous activity which is wrong.
such cycleing may happen for various reasons, thats why - even thought i'm aware of the supposed to be lifecycle of android - i wan't to EXIT (& destroy) the app when pressing back within a defined activity.
calling finish() dosn't help. if there's a previous activity it will re-open it. calling system.exit(0) isn't nice to do.
so: whats the right way to prevent such back-press-cycles and/or exit a application (WITH destruction)?
for better illustration of what i want to achieve: consider A, B, C being activities. a arrow (-->) illustrations a new intent call from the activity leftside of the arrow, rightside of the arrow represents the activity that is called. ex.: A --> B means activity A starts activity B. now here's what i want:
1) A --> B --> C pressBack:--> B pressBack:-->A pressBack:--> Exit
2) A --> B pressBack: --> A pressBack: --> Exit
3) A --> B --> A pressBack:--> Exit
as you see, back works as always, BUT when in activity A it exits the application.
the behaviour i got now is 1) and 2) as above but
3) A --> B --> A pressBack:--> A pressBack: --> Exit
keep in mind, i've already overwritten the onBackPressed listener of activity A with a finish() call. even calling system.exit(0) dosnt work. however, even if it would: its not what i want, i want the REAL way to do it android style - i cant imagine system.exit(0) is best practise.
Well this is the default behavior.
If you have another approach, just implement it.
One approach to deal with this is to use the android:launchMode="singleInstance" for activities that can be launched in a singleton manner (only one activity can exist)
For example, if the SMS page in the SMS app was a singleTop, it would have needed only one back press to remove all the SMS pages. It is a matter of choice
Another more aggressive way would be to finish Activities when you start another activity. Of course, such decision would risk making the app less friendly (android users are not accustomed to this behavior). Nevertheless, if this is used only where it may be considered acceptable then it might be acceptable.
A very acceptable place to do this would be a login screen: Once login is successfull, you start another activity (probably designed for logged in users) and finish the login activity.
Enjoy Finally, in my personal opinion, you can add an Exit button. Users will find it nice.
Check my post: Adding an Exit button to Android Application

Android back-button-overriding etiquette / guidelines

I have an app in which the user logs in from a main activity, and then can browse through a heirarchy of entities using listviews. So, the Activity stack would look something like this:
A -> B -> B -> B -> ...
where the number of B's is proportional to how deep you are in the tree of entities.
Frequently, I find myself pressing backbackbackbackback to get to the root (the first 'B'), but one too many presses and I log myself out, or even leave the app. I'm considering overriding the back button so that, when pressed from the root B, it will pop up a dialog essentially saying "Log out? (Y/N)", thus blocking a string of back-presses from completely exiting the app.
I've noticed a sort of sensitivity regarding overriding the back button, though, and - while it makes sense to me - I want to know if this is considered a good use of the back button.
So:
Would this be considered an appropriate/conventional override of the back button?
Is there a better/more conventional way to accomplish this without overriding?
Also, so this question might be more generally useful in the future, are there any guidelines for what is acceptable/unacceptable when overriding the back button?
I would find this use acceptable; I've seen a number of apps that ask for a confirm before exit - if the user wants to exit an app, they usually will press the Home button and let Android handle the finish() if and when it's needed.
I know I've accidentally exited an app by pressing back too many times :(
Dotmister's comment about Handcent is spot on - the back button should feel natural to the user; your use seems to adhere to this, in that a user will cycle back through activities as expected for the most part. As he said though, give it a try and test it.
Coincidentally, I have a similar flow in my app, but I've included a button for the root activity.
No it is not normal to overide the back button because the user ecxpects the back button to function as a... ?? back button.
On the other side if it is really that annoying using the back button in the normal way, than a compremise? will always be better. But make sure the user still has the idea he 'controls' the device according his rules, do not make him look for yours. (dutch way of making a point, sorry).
The only way to find out is getting some people to try your application and see if it's annoying or not.

Categories

Resources