In my application there are 4 activities.
1-LoginOrSignupActivity(Main)
2-SignupActivity
3-LoginActivity
4-MainFeedActivity
Problem is that when i am logged in and i am in MainFeedActivity if i press back but it takes me back to loginOrSignupActivity. I want my application to go on onPause State when back button is pressed within MainFeedActivity.
Flow is like LoginOrSignupActivity->LoginActivity->MainFeedActivity
other flow is like LoginOrSignupActivity->SignupActivity->MainFeedActivity
i used this Flag but still it is not working
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
If you first go to LoginOrSignup, then LoginActivity, and then open MainActivity, you need to call finish() on your passed activities when you move on. Or else the activity will remain in the back stack. Alternatively you can set a flag on your activity in the manifest.
Relevant reading: http://developer.android.com/guide/components/tasks-and-back-stack.html
For those cases where you don't want the user to be able to return to an activity, set the element's finishOnTaskLaunch to "true" (see Clearing the stack).
Related
Imagine you have the following sequence of activities:
Activity A -> Activity B -> Activity C
When you are on Activity C, pressing the native back button, takes you to Activity B. Now what is the state of Activity C? Is it still in memory or it has been finished?
If it is still in the memory, is there a way to resume the activity? Other than starting another instance of this activity...
I should add that this is the standard case where you do not use any flags including: FLAG_ACTIVITY_CLEAR_TOP
Default behavior is that when you press hardware "back" button, current activity will be removed from the backstack and activity "destroy" sequence will be initiated. From that moment you should not rely on the fact that it might be somewhere around - it is all up to Android to decide when does it actually kill this activity.
What my previous investigations show is that victim's onDestroy() will be called only when new activity is done loading and is idle.
You can specify android:launchMode="singleInstance" for your activity in Manifest. This will ensure that only one instance of activity is created at the time
You might want to consider reading the official docs.
More specifically the part that answers your question:
When the user presses the Back button, the current activity is popped from the top of the stack (the activity is destroyed) and the previous activity resumes (the previous state of its UI is restored).
Now for your second question… you can keep reading the same page…
when you start an activity, you want to bring forward an existing instance of it (instead of creating a new instance on top of the back stack)
So if you read that… you will find…
You can do these things and more, with attributes in the
manifest element and with flags in the intent that you pass to
startActivity().
In this regard, the principal attributes you can use are:
taskAffinity
launchMode
allowTaskReparenting
clearTaskOnLaunch
alwaysRetainTaskState
finishOnTaskLaunch
And the principal intent flags you can use are:
FLAG_ACTIVITY_NEW_TASK
FLAG_ACTIVITY_CLEAR_TOP
FLAG_ACTIVITY_SINGLE_TOP
No, it is not in memory. It gets finished when you press the back button. You should use android:launchMode="singleTask" in the androidmanifest.xml for that particular activity for which you want no new instances to be created if an instance is already there. For further information this link will be helpful.
In the following sequence...
Activity A -> Activity B -> Activity C
When you will press back button in Activity C then onBackPressed() method will be called. The default behavior of onBackPressed() is to finish the current activity until you Override the functionality.
So, in normal case, after back press the current Activity will be destroyed and you can't find it in the Activity Stack, so you can't resume it.
You will find more information from Android Developer's doc in below link...
Tasks and Back Stack
I currently have an Android application when in the process of browsing a hierarchy of objects creates a back stack like this.
BrowseActivity(Starting Instance) -> BrowseActivity(Instance B) -> BrowseActivity(Instance C) -> ViewObjectActivity
There is a menu button in the view object activity that I would like to have take the user back to BrowseActivity(Starting Instance) and destroy the rest of the browse activities.
Using Intent.FLAG_ACTIVITY_CLEAR_TOP only destroys the ViewObjectActivity and leaves the rest in the back stack.
Setting the BrowseActivity to "singleTop" in the manifest breaks the ability to create another instance for further navigation.
Any way to accomplish this that I am probably overlooking?
If you want the menu option to take the user back to the first activity, do just that - take the user back to the first activity.
Have your third activity exit with a specific result that tells the second activity to exit as well. You can extend this to how many activities you want - as long as all the activities except the first one exit, you're good.
You need to use this flag in a pair. The following has always worked for me
Intent.FLAG_ACTIVITY_CLEAR_TOP
Intent.FLAG_ACTIVITY_NEW_TASK
I have application with main activity and some more.
On each other activity there is the logo of the application.
When user presses the logo button, I want to get back to the main activity.
I do not want to create new intent, since activity aready on the activity stack.
How can I get it - use the one on the stack?
How can I clear the whole activity stack, so back button will actually exit from the application instead of getting back to the previous activity?
Yoav
I do not want to create new intent, since activity aready on the activity stack.
If you start an activity (via intents or any other way) which was already started and is on the stack , then Android just takes that same instance of the activity and places it on top of the stack. A new instance is not created. Ofcourse this happens if you did not manually kill the activity (by calling finish() in it).
How can I clear the whole activity stack, so back button will actually exit from the application instead of getting back to the previous activity?
Its not recommended to override the back button to quit the application in every activity(Unless your app has strong reasons to do so). generally the app should let the user go back to the previous activity when he presses the back button (which is what a user might be expecting).
If you still would like to quit with the back button then you can override the back button function and launch the intent that leads to the home screen:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
I faced a somewhat similar problem. The following link might be helpful for you:
clearing stack of activities with just one press
Its very simple.
Don't call finish() on Home/Main activity.
For Ex: Say you have 4 activities.. If your requirement is like this .. Act1-->Act2-->Act3-->Act4-->Act1 . So, don't call finish() on Act1. But call finish() on Act2, Act3 while you are going to other activity. So when you click on logo in Act4, just call finish(). So, automatically you will come back to Act1 which is your Main activity.
If you have logo in Act2, Act3 also then call finish() on click of logo to go back to Main. But remember to call finish() on Act2 while you are going from Act2 to Act3
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.
I already know that I can pass Bundled data through setResult from one Activity back to another. However, suppose I have an global Activity that can be launched from anywhere in my app since it is mapped to a button that appears in my title bar in almost all of my activities.
Long story short, after it completes its user-driven process, I want it to signal back to the very first activity in the back stack, basically my Home activity, so that it updates the UI accordingly.
Part of the problem is that since if I use a BroadcastReceiver, it is unregistered when my activity is in the background, and it will not get the signal to refresh its data set.
What I want to achieve is the following:
From either Home (ActivityA), or any other activity (Activity B, C, etc...) that can open out my global activity (ActivityX), it should find a way to call back to ActivityA without bringing it to the front.
Should I use FLAG_ACTIVITY_FORWARD_RESULT and if so, how should I model it from my subsequent activities after Home. In other words, if I launch a child activity from Home, should I launch it with startActivityForResult with whatever request code I define and then pass FLAG_ACTIVITY_FORWARD_RESULT when opening my global activity so that the result will be set from there?
Also, suppose I launch a child activity from Home with a result, and then from my child activity I add more to the stack, from which I open ActivityX. Would the system still remember the result chain as long as I opened the first child from Home with a result?
EDIT: I am not looking for just clearing the entire stack back to home immediately after the process is completed in ActivityX; just a way to signal the Home activity to refresh it's UI when the user eventually returns to the Home screen. I guess probably setting a SharedPreference flag that Home checks in onStart when the user re-focuses on that Activity which in turn gives me the condition to do the end result, after which the flag is reset.
Your home-screen should just update it's UI in onResume, this way whenever a user returns to it will be displaying the latest data. There's no need to pass callbacks. Otherwise you could register a Broadcast receiver in onCreate (and unregister in onDestroy... not ideal) in your home activity and then send out a broadcast when you want the home activity to update (although the home activity shouldn't actually update itself until it is resumed).
Here's how to get back to your home activity:
Intent goHome = new Intent(getContext(), HomeActivity.class);
goHome.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
goHome.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(goHome);
FLAG_ACTIVITY_CLEAR_TOP: This ensures that when the activity is launched, it displays its initial activity.
FLAG_ACTIVITY_NEW_TASK: we're starting a new task (i.e. the back button should not go back to the previous screen so that pressing back at your home screen will exit your app).
I'll update on the rest tomorrow.