Android back button and resume activity - android

I have this issue.
I have an actvity A that starts other activity B (by onclick - button).
In B I have one back button to come back to activity A. I press it.
Now that I'm in A, I press again button to go to B.
If I use android back button (I'm in B) I come back to A first and then to B.
But now, if i press android back button again, I don't go to previus activity of A or it exit from app. I come back to B !!!
How can I prevent this behavior ?

The back button of activity B should not start activity A, but close activity B with finish:
http://developer.android.com/reference/android/app/Activity.html#finish%28%29
Note that it is probably bad user interface design to have a "back" button on the interface. This official Android page says "Don't use labeled back buttons"
http://developer.android.com/design/patterns/pure-android.html

if you wish your custom back button to swap between the acivities A and B, you should simply launch intents, that will do.
I believe your problem is with the android back button. If you do not want go to the previous activity with this action, you can write in the manifest file under the <activity> tags for both A and B activities - android:noHistory="true". Doing this will exit from the app. as the activity stack was storing none of the activities, but such a requirement is quite confusing UI approach.

Related

Relationship between android:noHistory and android:finishOnTaskLaunch

How do these two attributes relate? If I have android:noHistory="true", does having android:finishOnTaskLaunch="true" have any significance/meaning?
Let's say you have three activities in your app: A, B, and C.
You start your app and see A, click a button and see B, click a button and see C.
First scenario
Now if you press the Back button on your phone, you will see B.
Second scenario
Let's say that B has android:noHistory="true".
Now if you press the Back button on your phone, you will see A. The android:noHistory="true" attribute removed B from the history (i.e. the activity stack), so you will not see it when you hit the Back button.
Third scenario
Let's say that C has android:finishOnTaskLaunch="true".
Now if you press the Home button on your phone and then launch the app again, you will see B. Android ended C when you launched the app again because it has the android:finishOnTaskLaunch="true" attribute.
finishOnTaskLaunch would kill the Activity as you move to another task. But noHistory would kill the Activity if you move to another activity in the same task.

Always have Activity on bottom of back stack

I have an App with a Navigation Drawer design and some activities (I know working with Fragments would probably be easier here but that wasn't feasible).
When the user clicks a drawer item, the according Activity is started with FLAG_ACTIVITY_REORDER_TO_FRONT. The up button should always take the user back to the start activity A, so when the up button is pressed I also start the A activity with the same flag.
Consider the following (capital letters are the activities, trying to visualize the backstack here):
A
user starts B from the drawer
A -> B
user presses up button, brings A in front
B -> A
user presses back button finishing A going back to B
B
user presses back button again which exits the application, but imho the user should always see the start Activity A again before leaving the app.
How would / did you guys solve this?
EDIT: #Neil, with that is: If I have
A -> B
and from there via drawer go to C I have
A -> B -> C
If the user now presses the Up button and I just close C instead of bringing A to the top, the user will be back in B which would be wrong because C is (navigationwise) not under B but they are 'siblings'.
Try to make the launchMode of that Activity B to SingleTask in your manifest file like this :
android:lanchMode="singleTask"
Your activity will not be called again.

How to make launch activity always as home activity

I have 3 activities in my app. A is the launcher activity, when I press on a button Activity B will be started, then I have a button to start Activity A or I can press back button and I can go to A. On back press previous values retained in and on button click new values will be set on A.
When I click a button in B , A will be started, I am using REORDER TO FRONT flag and singletop as launch mode. New activity is not getting created . When I press back button on A it will transit to B and again I press back button app exits. I want to have activity A on back press on B. I cannot do anything in onBackpressed() in B as B is used in several scenarios apart from the above mentioned scenario. How to manage it.
I have nor clearly understood what you are trying to do, but you could try the up-navigation pattern (http://developer.android.com/design/patterns/navigation.html#up-vs-back).
You would have to declare in your manifest that activity A is the parent activity of B and then use NavUtils.navigateUpFromSameTask(this); in your button OnClickListener.
(NavUtils is in the support.v4 package).

Android Activity back stack and multitasking support

I have an app that supports multitasking (working in the background), however I have run into problems with the android backstack.
This is what I have:
Activity A starts Activity B for result, so...
Activity A --> Activity B
If when at Activity B the user long presses the home button and switches to another application (say the browser for example) and then long presses the home button again and comes back to my app, they will be at Activity B, however the back stack at this time will look like this:
Activity A --> Internet Browser --> Activity B
So when I do finish() to send back a result from my Activity B it does not come back to my Activity A, but rather to the Internet Browser...
This is also the case if the user doesn't use long press of the home button, but also uses the home button to come back to their launcher and then uses long press home button to come back to my app. In this case the back stack is even worse:
Home Launcher --> Activity B
So when I do finish() on Activity B, the user gets back to their home screen and they can never get back to Activity A except for if they go and start the app again from their app drawer.
Is there any way to implement multitasking work in this case? Activity B needs to always return back a result to Activity A no matter what the user opened in-between these two.
OK. After long hours of research and trying various things, here's the solution to the problem. Hopefully this helps others...
The solution is pretty straight forward and simple, in AndroidManifest.xml
set android:launchMode="singleTask" for Activity A
set android:noHistory="true" for Activity B
This way Activity B will be removed from the Stack if we go to another app like the browser or exit to the home screen, so when we come back to our app we get back to Activity A.

Android - finish activity and exit app

I have an application which consists 3 activities - lets call them A, B & C.
From activity A You can get to activity B, from B to C and from C to A (By pressing the back button).
On activity A I have an exit button (I know it`s not recomandded on android, but a lot of users are asking for it, so I decided to add it).
Anyway the problem is that: activity B has a timer, when the timer is finished it starts an intent that starts activiy C and calls finish() on activity B.
When the user then press back on activity C he gets to activty A - and then when he presses the exit button (this button only preforms finish()), it pops up activity B again. How can I prevent it from happen?
Thanks!
Idea: set a isFinished field in your Application to true and call finish(). All activities check if isFinished in their onResume() and if true they finish().
I'm not sure I understand how you end up at A when going back from C. Shouldn't it go back to B (logically, I mean. I know you want it to go to A)? In fact, that seems to kind of be what's happening. B is getting relaunched, but A appears on top of it, so that when you finish A, B (which was under it) is now shown.
It sounds like you don't want B to be part of the history stack, so maybe when you launch B, you should launch it with the no history flag.
You activity b is not finished thats what i can figure out it is still in running state so when all other actitivties are finished it shows up please chekc if it is finished or not

Categories

Resources