I need to start an activity which will be added into the overview screen (recent task list).
I found this document and follow the guide:
https://developer.android.com/guide/components/activities/recents.html
In activity A, I add "FLAG_ACTIVITY_NEW_DOCUMENT" flag to my intent and start activity B with it.
I see both A and B in the list, but when I close activity A, I can still see B in the list. And when I go back to activity B, it seems to be destroyed and create again. (it looks like the activity has been restarted)
My question is:
How to automatically close B after I close A?
Is there any way to prevent the restarting process?
simply call finish(); after starting your Activity
Related
I'm new to Android development and I have an app with various activities. For performance reasons I'd like to properly manage the activities when users are using my app. Here's my problem.
Activity A - starting activity with map
Activity B - user navigates to Activity B (a list view) from Activity A.
The user then selects the map icon to navigate to Activity A again.
So if you can image it, my activity stack is now:
Activity A
Activity B
Activity A
So if I press the back button the device it will take forever as it scrolls through activities.
Is there a way of managing this so the old activity is destroyed and is just re-created upon choosing an activity nav icon. I've read up about onDestroy() and onStop() but I'm a little confused at their implementation.
Apologies for a poorly worded question but I'm unsure of the correct lexicon to ask about activities.
One simple solution is to kill the Activities as soon as they leave the foreground.
You could do that by calling finish() inside onPause().
You could have B finish itself and return to A instead of starting another A. Or, if your stack might be more complicated, like this:
D
C
B
A
D could start A with FLAG_ACTIVITY_CLEAR_TOP, which would cause D, C, and B to be finished, leaving A on top. (That intent flag interacts non-intuitively with a couple other flags, so read the docs.)
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
Scenario:
Activity A (MAIN and LAUNCHER in manifest) starts up when clicking on launcher icon.
In turn it launches Activity B.
Activity B then launches our main app Activity C (MAIN and singleTask in manifest).
Behaviour I require:
Once Activity C has been shown and the home key is then pressed, the next time the launcher icon is pressed I would like to skip straight to Activity C (and not show Activity A (and consequently B) again).
I have tried using FLAG_ACTIVITY_CLEAR_TOP from A, but I still get Activity A whenever I hit icon on launch screen.
Is appearance of my singletask Activity C from launcher achievable?
Update: Use of FLAG_ACTIVITY_CLEAR_TOP from A and not calling finish() creates the situation whereby Activity B appears on press of launcher icon. However, also applying use of FLAG_ACTIVITY_CLEAR_TOP from B and not calling finish() does not resolve situation. now I don't get A on launcher icon press, but get B. What gives!
See similar scenario here.
In your case, I would recommend using a similar approach, where you would have a SharedPreference that would persist a value representing whether your app had previously been launched.
Create a simple Activity that will contain the logic for what you are trying to do here. In that activity's ("pre-A" Activity) onResume() method, check the value of the preference representing whether the app has ran previously.
If it has not previously been ran, launch an intent to Activity A, which calls the subsequent B and C activities; otherwise, launch an intent to Activity C.
Seems pretty straightforward, just remember to define the new "Pre-A" activity in your manifest!
I was going through Android Task and Back Stack documentation and at one point they mention this:
if your application allows users to start a particular activity from more than one activity, a new instance of that activity is created and pushed onto the stack (rather than bringing any previous instance of the activity to the top). As such, one activity in your application might be instantiated multiple times (even from different tasks), as shown in figure 3. As such, if the user navigates backward using the Back button, each instance of the activity is revealed in the order they were opened (each with their own UI state)
Let's take an example:
I have Activity A Starting Activity B which Starts Activity C which starts D.
Stack is A->B->C->D now it is possible to Start C from D so when we start C from D stack will be
A->B->C->D->C
Now instead of this standard behavior I want Activity to have only 1 instance or only 1 entry in the Back Stack. "SingleTop" will not work since Activity C was not on top when we started it from D.
I might be missing something but is there any way to achieve this making sure an activity has only 1 backstack entry?
Thanks
Pranay
Use Intent.FLAG_ACTIVITY_CLEAR_TOP, e.g.:
Intent intent = new Intent(context, <your_activity_here>);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(intent);
So, all the activities in stack after activity C will be finished automatically. If you use the specified flag
A->B->C->D
will become
A->B->C
You can also use android:launchMode="singleInstance" in your activity tag in manifest
I have a main activity A. There are two scenarios
1) A launches B.
B has launchmode singleTask and is launched with FLAG_ACTIVITY_NEW_TASK.
now I have a menu option in B which performs a delete operation and starts the activity A.
2) A launches B, which launces C it also contains the menu option to perform delet opereation.
I want A to be started with clearing the stack in both the scenarios but the activities belonging to another task still present there
I am stuck is there a way to clear the stack.
try using following code on delete opereation on both B and C activity
Intent intent=new Intent(B.this, A.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
If i talk about your second scenario then FLAG_ACTIVITY_TOP_CLEARwill clear the stack..
And your stack now will be only A instead of A-B-C-A.
and in second case AFAIU your problem you have only two activity A and B so if you want to restart A then after restarting A manually finish B.
Hope you got some trick.
Another thing if you have activity with launchmode=SingleTask then you can use flag_activity_brought_to_front.
That will act like
A-B-c to A
Why not finishing B and C before calling A again?
finish();
My method can not meet your goal A to be started with clearing the stack,But when user choose delet opereation and start B from A again the task that include A and B will be reset.
Use the flag FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET to mark the task will be clear when needed when you first start activity B from A, then if user choose delet opereation menu item from B or C, you set a flag, so next time from A(A should be single_task launch mode in manifest), you use the flag FLAG_ACTIVITY_RESET_TASK_IF_NEEDED to start B. This flag will clear B and all top of the B.
Hope this will help you :)