I'm following this guide to show my activity in Recent Screen like a separate task.
I've added this flag to open my activity in a new task:
newDocumentIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
When I start an activity using startActivityForResut the new activity is not opened as a separate task.
However, if I start an activity using startActivity it is opening in a new task.
Is this is the default behavior? or am I missing something?
See the documentation for FLAG_ACTIVITY_NEW_TASK:
This flag can not be used when the caller is requesting a result from the activity being launched.
So I guess if you're asking for a result, android forces you to stay in the same task.
Related
To avoid OnDestory() and OnCreate() beeing called all the time when navigating between my main activity and some sub activities, I have set the singleTop option in my manifest. Unfortunatly this causes that the UI in my main activity is gone when returning from a sub activity. Do I really need to redraw my UI manually?
I am still wondering how to handle these basic navigation features. Is it unusual to perform application initialization tasks (e.g. start services) in OnCreate() of the main activity?
I must be missing something from your explanation I guess because the very basic point that you provided is not correct. When you launch another (sub) activity from your activity then your starting activity is not destroyed but it is stopped. So your onStart is called when you navigate back.
So maybe you can explain a little bit more the background of your requirements.
Use android:launchMode="singleTask" for your main activity so the system will not re-create it if you call it via Intent. You can also clear the stack and go back to your main activity by using
Intent intent = new Intent(SubActivity.this, MainActivity.class);
intentHome.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intentHome.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intentHome);
Is there a way to launch an activity such that it displays in a similar way to "launchMode=singleInstance" but still can launch other activities using startActivityForResult?
I want to start an activity in dialog form from a widget but I don't want to bring up the main application if it's running.
Thanks!
I think you're looking for the FLAG_ACTIVITY_NEW_TASK flag.
Just set this flag on your intent and you should get the behavior you want.
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
NOTE: It will only create a new task if the activity you're trying to start isn't already running in a task. Otherwise it brings that task to the front.
does the launchMode of the launcher activity in the manifest get ignored?
The android documentation says that the default launchMode is "standard" but this isn't logic for me if this would be applied to the main activity of an app because each time you start the app, another task would be created in the instance of the app.
Well, I delved into Android sources myself and found the following thing.
The launcher starts apps using the method startActivityAsUser in LauncherAppsService. The intent is constructed using these lines:
Intent launchIntent = new Intent(Intent.ACTION_MAIN);
launchIntent.addCategory(Intent.CATEGORY_LAUNCHER);
launchIntent.setComponent(component);
launchIntent.setSourceBounds(sourceBounds);
launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
According to Android documentation, the flag FLAG_ACTIVITY_NEW_TASK means:
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.
This effectively and unconditionally overrides launchMode specified (or omitted to default behaviour) in the app, and ignores this attribute.
I think this demonstrates that the documentation is not clear (or complete) enough. Without such deep investigations of the core source codes everyone can get unexpected results now and then.
You are confusing two things. One is launchMode and the other is "what happens when the user selects an app icon from the HOME screen, or selects a task from the list of recent tasks". These are 2 completely different things.
launchMode
Each Activity has a specified launchMode (the default is "standard" or "multiple". This tells Android how to start this Activity, and there are many factors that can contribute to the "interpretation" of the launchMode. It depends on what other flags may have been specified in the Intent used. It depends on which task requested the launch of the Activity (or if the launch was requested from a non-activity context, like from a Service or BroadcastReceiver). It depends on whether or not an existing instance of the Activity is already active in the specified task, etc.
Behaviour on selecting an app icon from the HOME screen or list of installed applications
When the user selects an app icon, startActivity() is called with an Intent containing the following data:
ACTION=MAIN
CATEGORY=LAUNCHER
Component is set to the package name and the class name of the Activity that is defined in the manifest with ACTION=MAIN and CATEGORY=LAUNCHER
Flag FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_RESET_TASK_IF_NEEDED are set.
Regardless of the launchMode definition of the Activity to be launched, calling startActivity() with an Intent like this causes the following behaviour:
If there is already an existing task whose task affinity matches the Activity being started (in simple terms, if the app is already running), Android will simply bring the existing task to the foreground. That's it. It doesn't create an instance of any Activity. It doesn't call onNewIntent() on any Activity. It does nothing other than bringing the existing task to the foreground. This is why, even if you specify launchMode="standard" for your launcher Activity, Android doesn't create a new instance every time you click on your app icon.
If there isn't already an existing task whose task affinity matches the Activity being started (in simple terms, if the app isn't already running), Android will create a new task and launch the Activity into that task. launchMode doesn't play a role here, since there is absolutely no difference between the launch modes when launching a single Activity into a new task. Android always creates a new task and always creates a new instance of the Activity as the root of that task.
This behaviour is also the same when the user selectsa task from the list of recent tasks. If the task is still running, Android just brings the task to the foreground, does not start any new Activity instances and does not call onNewIntent(). If the task is no longer running, Android creates a new task and launches the launcher Activity into that task. The only difference here is that the flag FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY is also set in the Intent if the user selected a task from the list of recent tasks.
I hope this answers your question.
See this answer for a very detailed explanation of FLAG_ACTIVITY_RESET_TASK_IF_NEEDED and task reparenting in general.
Think of everything except the opening activity as an abstract implementation. Declaring an activity as
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
Will cause it to open first. Subsequent activities are Overriden at the time an Intent is formed to navigate between activities. The overrides are represented as intent flags.
A list of intent extras:
http://developer.android.com/reference/android/content/Intent.html
With flags being commands you'd otherwise have written in the Manifest.
You are right.The default mode is "standard".
According to android documentation
*In standard mode ,Every time there's a new intent for a "standard" activity, a new instance of the class is created to respond to that intent. Each instance handles a single intent.
*.If the parent activity has launch mode standard (and the up intent does not contain FLAG_ACTIVITY_CLEAR_TOP), the current activity and its parent are both popped off the stack, and a new instance of the parent activity is created to receive the navigation intent.
The behavior of Activity set to standard mode is a new Activity will always be created to work separately with each Intent sent. Imagine, if there are 10 Intents sent to compose an email, there should be 10 Activities launch to serve each Intent separately. As a result, there could be an unlimited number of this kind of Activity launched in a device.
Behavior on Android pre-Lollipop
standard Activity would be created and placed on top of stack in the same task as one that sent an Intent.
For example, when we share an image from gallery to a standard Activity, It will be stacked in the same task as described although they are from the different application.
If we switch the application to the another one and then switch back to Gallery, we will still see that standard launchMode place on top of Gallery's task. As a result, if we need to do anything with Gallery, we have to finish our job in that additional Activity first.
Behavior on Android Lollipop
If the Activities are from the same application, it will work just like on pre-Lollipop, stacked on top of the task.
But in case that an Intent is sent from a different application. New task will be created and the newly created Activity will be placed as a root Activity like below.
Source from here
The first activity of my application is a splash screen, where some animation is displayed while some loading occurs in a background thread using AsyncTask.
Once the loading in the background is done, I want to start a new activity. What is the correct way to do that ?
Start a new activity directly from the onPostExecute method of the AsyncTask class.
Check if the current activity is displayed before starting the new activity :
If the current activity is display, start the new activity.
If the current activity is NOT display, use a flag (boolean) and check the flag during the onResume method in order to start the new activity there.
My main concern is :
If my application went to the background (due to an incoming phone call, a home key press, ...) and the background thread (AsyncTask) finished executing, and a new activity is started from the onPostExecute method while my application is still in the background : What happens ?
Will the new activity start directly as soon as my application is visible again ?
I faced a similar situation: my application went to background and after some time the app started an intent displaying another activity. However, the app's behavior now depends on the os that it's running:
on pre 4.4 devices the app silently opens the new activity and remains in background. When the user resumes the application, he is prompted with the second activity already.
on 4.4 devices (tested on 4.4.2 and 4.4.4) the app opens the second activity, but after 3-4 seconds, the app pops to foreground, interrumping the user.
Hope it helps anybody. I'm still looking solutions for the second case in order to prevent the app from popping to foreground.
From my experience i am answering your question
Question1
If you using AsyncTask you have to start new activity in OnPostExecute(). In my experience this is the correct way of doing it.
Question2
When ever your press the home key or receiving phone call. Your activity will go in to background until you exit the app by pressing back button(at that time your app is exited). So when you come back to your app your new activity will be visible if background process get finished. Otherwise you will see the start splash screen.
I think that it is dependent upon the OS of android. It has defined some built in priority model for each of the components.
Look at the answer given by commonsware.
change process priority in android
this gives brief idea about components priority.
I have created an activity. I made the launch mode as singleTask in manifest file. I donot want multiple instance of the same activity should start. I am lunching this activity on button click of another activity.
If i click the button more than once then as i have made the activity as single task then If the activity already running then nothing happens. But I want to relaunch the activity without creating another instance. How to achieve this.
Thanks
Deepak
But I want to relaunch the activity without creating another instance
What you meant my relaunch without creating. I think you may have set of code that you have placed in your onCreate and you want to run when the activity again got control. If so its better for you to place that code to your onResume or onNewIntent. Because onCreate of the single task will only execute once
singleTask
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.