android activity always on top - android

There are 2 activities: A and B
if activity A is started then it will be always on top of activity B (if B exist)
if B is started and A is in foreground then B will go right under A
if A is going in background or is closed then(if B exist) B will be shown
each activity is running in its own task
In other words is there a simple way to start an activity right under the current activity so when that close then it will be shown
what is the simplest way to do this using manifest flags preferably

Start A inside new Task stack. Read docs for Intent.
Inside onCreate of A check Intent and open B.
Now you have new stack where B is above A, however and A and B were used before.

Related

The behavior after adding FLAG_ACTIVITY_NEW_DOCUMENT flag

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

Go back to Activity without recreating (without invoking onCreate())

I would like to ask similar question to:
Go back to previous screen without creating new instance
However I don't want Activity A go through onCreate callback, I would like to return to previous instance in the same state. How to achieve that without calling multiple finish() ?
There's no need to finish() activities as you navigate through your application. Instead, you can maintain your Activity back-stack and still achieve your goal. Let's say you have 4 activites like so:
A --> B --> C -->D
where D is the topmost activity and A is root activity. If you are 'falling back' to activity B, then you'll need to do two things in order to avoid hitting B's onCreate method.
1.) Make B a "SingleTask" activity. You can do this in the Android Manifest. To be brief, this means that only one 'instance' of B will ever exist within this Task. If B is already running when it's called then it will simply be brought to the front. This is how you do that.
<activity
android:name=".ui.MyActivity"
android:launchMode="singleTask"/>
But you don't want to just bring B to the front. You want to 'fall back' to B so that your stack looks like
A--> B
2.) Add this flag to your intent that 'starts' B. This ensures that C and D are removed.
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Now when 'D' calls new Intent to B, B will be resumed and C and D will be removed. B will not be recreated, it will only call onNewIntent.
When navigating from A to B do not finish Activity A.
When navigating from B to C finish Activity B.
When navigating from C to D finish Activity C.
So from wherever you navigate back finish current Activity and Activity A will get resume again not get created.

Use FLAG_ACTIVITY_NEW_TASK to bring existed task to front

The android document of FLAG_ACTIVITY_NEW_TASK said :
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.
I make an demo to implement this, There are two app: A and B. A contains activity A1, B contains activity B1, B2.
I follow the step :Home Screen(Application A icon) --> A1 (FLAG_ACTIVITY_NEW_TASK to start B1)--> B1 -->B2 --> Home Screen(Application B icon)--> , as my understanding, click the application B icon from launcher to launch the Activity B1 will use the FLAG_ACTIVITY_NEW_TASK, right? So it should bring the task started before to front, but unfortunately the fact is an new activity B1 is started and the B task stack is like this : (Bottom)B1 B2 B1.
How can i let the existed task brought to front when i click the application B icon in the launcher
A and B are two different apps, Right ?
That means both will execute as a separate process and behave as separate task.
so when you set FLAG_ACTIVITY_NEW_TASK to A it will create a separate task for A and make it as a root activity or brought to front the already existing instance of A.
When B is started from launcher, according to android normal launcher behavior it will start as a separate task and B will have separate stack.

How do I collapse "child activities"?

Example: I have an android app with 3 activities that has the following behaviors:
A (Home) -> B -> C
Activity A launches Activity B
Activity B launches Activity C
When user is on Activity B and they hit the Back button, it takes them Activity A
When user is on Activity C and they hit the Back button, it takes them Activity B
What I would like is when user is on Activity C, if they hit the "My Root Activity" button, it will take them to Activity A without adding a new instance of Activity A to the back stack.
So I don't want to have:
1) A
2) A-B
3) A-B-C
4) A-B-C-A
What I would like is:
1) A
2) A-B
3) A-B-C
4) A
How can I do this?
Check out the intent stack machinery!
http://developer.android.com/guide/topics/fundamentals/tasks-and-back-stack.html
EDIT:
You can add "singleTaks" or "singleInstance" to your activity in the manifest file and implement the onNewIntent() method.
"singleTaks"
The system creates the activity at the root of a new task and routes the intent to it. However, if an instance of the activity already exists, the system routes the intent to existing instance through a call to its onNewIntent() method, rather than creating a new one.
"singleInstance"
Same as "singleTask", except that the system doesn't launch any other activities into the task holding the instance. The activity is always the single and only member of its task.
Check out this link
I have each child activity extend a base activity. In the base activity, I defined an onActivityResult() which will impose a finish() if the activity at the top of the stack set a specific resultCode. So if they hit the "My Root Activity" button on Activity C, it will recursively roll up to Activity A. The Back button maintains it's functionality.
This is a rare scenario as I would have used "singleTask" but launching Activity A involves reloading it's dependencies which I don't want to couple of Activity C.

Go back to a specific activity

Here's what I want to do:
If I use startActivity I start a new Activity and I can't use StartActivityForResult from Main Menu -> Total. I would like to go back on the Main Menu activity already present in my stack and remove Activities A, B ,C and Total.
Have you tried using FLAG_ACTIVITY_CLEAR_TOP flag in your intent (used to start "Main Menu" from "Total")?
http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP
If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.
To remove activities A,B,C, and Total from your stack, just call finish() on these 4 activites.
See also : Tasks & Back stacks and Activity task Design

Categories

Resources