Actually i m little bit confused in Intent.
Suppose i have three activities.
A,b,c and in activity A i have exit button. When i click on exit button my application finishes. I have one more button in A which is next button. Which take me to new activity.
and in activity B i have two buttons next and back, and in activity C also i have two button out of which first takes me to A and Back button.
now i'm on C activity and want to go to A. where when i press exit it again takes me back to C instead of finish the application.
Why is this happening?
Not really answering your question but your Android application just shouldn't have an Exit button. It's not necessary.
This blog post by Reto Meyer - a Google employee who works on Android - explains it well. This passage from it might be significant in relation to your problem:
In most cases the exit button simply calls Activity.finish. This is exactly equivalent to hitting the back button. Exactly.
There is no Exit function in Android.
You probably want to bring up the Home application by it's corresponding Intent:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Exit button or not, Activity.finish only applies to the current activity and you are dealing with three different activities. Finishing activity A is simply taking you back in your stack to the previous activity C.
Check out the documentation on Activities and Tasks, launch modes, and clearing the stack for some explanation of what's going on in your example and what you can do to alter the behavior. I've always thought these sections of the Android documentation need to be enhanced or further explained but hopefully it will help a little.
Related
I'm making an application in android and I want to implement a button such that whenever it is pressed, I just reach back to my home screen. I know that we have the hardware key and soft keys( when there are no hardware keys) which implements this, but I want to add this functionality for this application. Does anybody has any idea how to do it?
Thank you
Try this
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
You can use an icon on your action bar and program it to bring you back to your application home screen.
Take a look at this section of the developers guide.
http://developer.android.com/design/patterns/navigation.html
The Android architecture is not ready to exit an Application with one line of code. You just cannot do it. Rely on your hardware buttons or make a finish() waterfall effect on all your Activities. You can use startActivityForResult() to start your Activities (all of them if you want this method to work). Then, when you want to exit your App, just call setResult(Activity.USER_CANCELED); and finish(); right after it. It will return to your previous Activity onActivityResult() callback. There, if the requestcode is the right one and the resultCode is equal to Activity.USER_CANCELED, just do the same: Call setResult(Activity.USER_CANCELED); and finish();. Once more, it will take you back to the previous Activity, if it exists. And so on, until you exit your app.
I do totally agree with the Navigation below
Imagine that the Book detail is made in different instances of a BookDetailActivity.
The stack before pressing up in book2 detail is:
BookDetailActivity (Book 2 - You are here)
BookDetailActivity (Book 1)
AllBooksActivity
If I follow the guidelines I will use:
Intent parentActivityIntent = new Intent(this, AllBooksActivity.class);
parentActivityIntent.addFlags(
Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(parentActivityIntent);
finish();
But the big problem with this code is that BookDetailActivity (Book 1) is still alive!
Pressing back button after "up" would bring the detail of Book 1.
How can I kill all the BookDetailActivity that are between the original AllBooksActivity and the activity where I pressed up?
The related guidelines article notes the following:
Implementation Note: As a best practice, when implementing either Home
or Up, make sure to clear the back stack of any descendent screens.
For Home, the only remaining screen on the back stack should be the
home screen. For Up navigation, the current screen should be removed
from the back stack, unless Back navigates across screen hierarchies.
You can use the FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_NEW_TASK
intent flags together to achieve this.
Since you're doing that, BookDetailActivity1 should be closed by FLAG_ACTIVITY_CLEAR_TOP. The only way if it could be alive and shown on pressing Back is if it would have been started before AllBooksActivity.
As for not needing FLAG_ACTIVITY_NEW_TASK (suggested by android developer's answer):
When using this flag, if a task is already running for the activity
you are now starting, then a new activity will not be started;
instead, the current task will simply be brought to the front of the
screen with the state it was last in.
...so if your activity exists, it will not start a new task.
ok , there are multiple ways to do such a thing. here's one of them:
however , i would suggest that instead of opening the first activity as if it's a new one , simply finish the current one and the one before it .
in order to do it , call each new activity with startActivityForResult , and set the result for each of the activities to some value that says you wish to return to the first activity .
you can even set the value to be the class canonical name , and make a base activity that will handle all of those requests automatically so that you won't need to handle it .
in any case , i think you made a mistake by using Intent.FLAG_ACTIVITY_NEW_TASK since it creates a new task , so the previous one still exists . try to read the available intents flags for more information:
http://developer.android.com/reference/android/content/Intent.html
I have 4 activities. A,B,C and D.
From each activity user can start any activity.
i.e. Activity A has 3 buttons to start B,C and D. In same way all other activities also has 3 buttons.
Now my question is If user go in this sequence A->B->C->B.Now If user press back button then this sequence happens. B->C->->B->A. I want to change in this way B->->C->A. how can i do this?
I don't clearly understand your question, but maybe try use:
android:launchMode="singleInstance"
You want to keep the same instance all the time, not create a new one, yes?
finish activity B using finish() when you moved in the forward direction that means A>B(finish())>C>B
Like #Krishnakant said you need to add finish(); everytime, As far as I'm aware the back button is called as the Achille's heel for android programming as coding or adding handlers to it is not easy.
use intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); before staring any activity.
I am trying to close a whole stack of activities using the way described here:
How to kill an application with all its activities?
Namely, each activity starts the other one with startActivityForResult, and in onActivityResult calls finish() to close itself together with the activity it opened.
The problem is that the activities in the task still seem to repaint themselves at least once before they close, and this doesn't look good. After closing the topmost activity one sees all previously opened activities like in a very fast slideshow.
How can one avoid this graphical issue?
EDIT: I need that if the user presses HOME button and then presses the app's icon in launcher, he returns to the current state of the stack, not to the very first activity again. So, from what I understand, with such a requirement I can't finish() activities before starting next ones.
That's native behaviour, intended to aid in user Experience. When an Activity is started with startActivityForResult and then finishes, it will (on devices that allow fancy animations) automatically slide away. That helps people not get surprised by the screen suddenly changing.
You could try starting the Activities without startActivityForResult and handling the passing of data to and from Activities manually, then handle how/when Activities finish() and which Activity they pass back to. You might find you implement something where Activities actually pass forward to the appropriate Activity all the time, rather than back to an Activity on the stack.
Intent intent = new Intent();
intent.setClass(getApplicationContext(),
PhoneListCheckboxAES.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
If u give like this when u are starting the next activity then the graphical problems won't occur
I have not really understood the handling of activities and the stack.
I have 3 activities, A - a splashcreen, B- a menu and C another Activity. I start the splash and exits it after a while when the menu is started.
In code I handle them all like this:
startActivity(new Intent(this, ContactInfoMenu.class));
finish();
Now, if I start the app and goes A-B-C, when I hit "Back" in C screen I jump back to B-the menu. Another "Back" exits the application, just like I want.
BUT .. if I go A-B-C-B-C - the "Back" button in C screen exits the whole app instead of getting me back to the B screen?
Why is that? It does like that in all my "subscreens", I can only enter them once, if I enter them a second time the "Back" button exits the app. And I have not tried to catch the "Back" action anywhere? Shouldn't I always call "finish()" when I start a new activity?
Regards
Finish is good for leaving the current activity and going back to the previous one. Otherwise, try to avoid calling finish() if you can help it.
There are a set of flags that you can pass when you start an activity that do a better job of determining how that activity behaves on the stack. These include:
FLAG_ACTIVITY_NO_HISTORY - your activity will not remain on the stack after another activity covers it.
FLAG_ACTIVITY_CLEAR_TOP - a good way to pop off a bunch of activities when you need to "go back" to a certain activity.
Many of these flags can be set in the manifest. Reading up on them will give you a better idea about "The Android Way".
Basically, You don't need to call finish() every time you go to another activity. If system is low on memory it will close your activity instance by itself.
finish() is more often used when yor are inserting some information in one page and then moving on to some other page. In that case, you might need to fininsh your first activity.
But in case where you need to shuffle between views, you must not use a finish() function, because it will cause the whole application to finish.
Try using back button of your own in your views to shift between activities, where you can move to any other activity of your application or even to the Main Screen.