I been told that starting activity from application context is bad idea, and you should prefer using activity context where possible.
Intent intent = new Intent(appContext, MyActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
appContext.startActivity(intent);
So I read Google article about tasks: Tasks and Back Stack.
It says that each stack got its taskAffinity id, and if new activity will be lunched with taskAffinity id that belong to already running task, than it will be placed in that task(if lunch mode is default or single top).
So if you didn't declare taskAffinity id for your activity, it will not matter if you lunching your activity from app context or activity context.
But once again, I been told that using app context to start new activity is a bad practice. So please tell me, why is that so? Do I missing important cases where I should pay attention to?
Related
I am trying to bring an activity to the front. I found many questions similar to this but none of them actually works.
I always hold a reference to the current activity in a variable in application class. While the application is running in the background (after onPause fires), if any message arrives, I need to bring the same activity to the front and display the message. The only way I got it worked is..
Intent i = new Intent(mCurrentActivity, JoboffersActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
The issue I have got with this, is that it recreates the activity which I wanted to avoid. I tried singleInstance and singleTask both in the manifest. If I do not add FLAG_ACTIVITY_CLEAR_TOP, on back key press it takes me to the previous instance of the same activity. Even if I add sigleInstance it creates two instances of the same activity. If I do not add FLAG_ACTIVITY_NEW_TASK then it shows an error Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag
Try to add another flag FLAG_ACTIVITY_SINGLE_TOP to your intent.
According to Android documentation, combination of this 2 flags:
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
brings the current instance of Activity to the front.
http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP
What happens when you click on an app's launch icon?
Is a new intent always sent, or is the result sometimes the same as resuming a task from recent tasks?
If an intent is sent, when does it get sent to the onCreate() method of a new activity instance and when does it get routed through onNewIntent() of an existing activity?
Let's suppose the intent gets routed through onNewIntent() of an existing activity in the task. Which activity does it get sent to? The one nearest the top or the one nearest the root? Will it always get sent to an instance of the application's launch activity or can it sometimes get sent to an activity with the same affinity as the root? Can it ever get sent to an activity which does not share the same affinity as the root?
Finally, how is this all affected by the various launch modes (standard, single top, single instance, single task) of the activities in the task?
If there is anyone out there who understands all this, please help me!
What happens when you click on an app's launch icon?
Launcher apps calls startActivity with an intent [action = Intent.ACTION_MAIN, category = Intent.CATEGORY_LAUNCHER and flag = Intent.FLAG_ACTIVITY_NEW_TASK].
Regarding Intent.FLAG_ACTIVITY_NEW_TASK, from docs:
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.
onNewIntent basics:
onNewIntent is delivered only when activity has set either singleTask, singleInstance launch modes. It is also delivered if activity has set singleTop launch mode or the intent to start the activity has set the flag FLAG_ACTIVITY_SINGLE_TOP and the activity instance is already at the top of the target task. It means an attempt was made to launch a new instance of activity, instead the existing instance itself need to handle the intent.
Here is the response to your queries:
Is a new intent always sent, or is the result sometimes the same as
resuming a task from recent tasks?
If the task is already running, it is brought to foreground. In case FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET flag was used to launch a activity and latter the task is brought to foreground, then the activity is killed. From docs:
This is useful for cases where you have a logical break in your
application. For example, an e-mail application may have a command to
view an attachment, which launches an image view activity to display
it. This activity should be part of the e-mail application's task,
since it is a part of the task the user is involved in. However, if
the user leaves that task, and later selects the e-mail app from home,
we may like them to return to the conversation they were viewing, not
the picture attachment, since that is confusing. By setting this flag
when launching the image viewer, that viewer and any activities it
starts will be removed the next time the user returns to mail.
-
If an intent is sent, when does it get sent to the onCreate() method
of a new activity instance and when does it get routed through
onNewIntent() of an existing activity?
onCreate is called while creating a new instance of activity. onNewIntent is called if already an activity instance exists and no new instance need to be created, as in case of singleInstance, singleTask and conditionally singleTop (as described above).
Let's suppose the intent gets routed through onNewIntent() of an
existing activity in the task. Which activity does it get sent to? The
one nearest the top or the one nearest the root? Will it always get
sent to an instance of the application's launch activity or can it
sometimes get sent to an activity with the same affinity as the root?
Can it ever get sent to an activity which does not share the same
affinity as the root?
In case of singleTask and singleInstance it has to be root of the task. In case of singleTop it has to be top activity of the task.
Finally, how is this all affected by the various launch modes
(standard, single top, single instance, single task) of the activities
in the task?
I hope the explanation provided till now, answers it.
Update 1:
Here is the Launcher code which adds the flags to intent:
void processShortcut(Intent intent) {
....
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
....
}
void startActivitySafely(Intent intent) {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
...
startActivity(intent);
}
Your best bet is to read through the Developer docs here: http://developer.android.com/training/basics/activity-lifecycle/index.html
There is a flow chart in the first lesson(http://developer.android.com/images/training/basics/basic-lifecycle.png) which provides an excellent graphical representation of the Android activity life-cycle.
I'm setting up Notifications in my app and I've noticed whilst testing, after clicking a new notifications (in this case the notification loads a blog details page), I have many instances of the blog details activity running (pressing back it shows each activity with the previously loaded blogs).
Is it possible in my Receiver class, so look if there is any instance of ActivityBlog already running, and if there all .finish() them all so there is only ever once instance running?
I found this but I couldn't work out a way to do it from that.
You should study activity launch modes
http://developer.android.com/guide/topics/manifest/activity-element.html#lmode
Use android:launchMode="singleTop" in element in manifest file.
You will get callback in onNewIntent() if an instance of activity is already up. Stack of your activities will be automatically updated and there wont be any need of killing activities which is consumes time and resources.
This i believe is the recommended approach.
Intent z = new Intent(Projects_Accel.this,MainActivity.class);
z.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_CLEAR_TASK |
Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(z);
use this for kill all activity
Do this way
Intent intent = new Intent(this, ActivityBlog.class);
ComponentName cn = intent.getComponent();
Intent mainIntent = IntentCompat.makeRestartActivityTask(cn);
activity.startActivity(mainIntent);
NOTE:
You need to put android-support.jar in libs folder
How do I get my app to appear in on the screen after it has been replaced by some other screen/activity? Some network event occurs and my application wants to reappear in the foreground (presumably in a polite manner).
I think I need to do something like:
contxt.startActivity(myActivity);
I don't want to create another instance of my app or cause it to restart, but I want it to appear.
Use FLAG_ACTIVITY_NEW_TASK
Intent intent = new Intent(contxt, myActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
contxt.startActivity(intent);
In your myActivity onNewIntent is called. I assume myActivity is the top activity in your app current stack
What is the difference between starting ActivityB from ActivityA using
1. startActivity(this, ActivityB.class);
versus
2. startActivity(getApplicationContext(), ActivityB.class);
I typically see 1. used more often in examples, but I haven't come across a reason for why this is the case.
Reference to Activity as a Context (this) might become obsolete if your Activity goes through configuration changes, like rotation, and is destroyed and created again. Context recieved by getApplicationContext(), however, persists through lifetime of the process.
However, It seems to me it only is an issue when you bind Activity to Service or other similar scenario, so it's safe to use this when you use it in intent to start another Activity.
There is no difference. According source code of Intent and ComponentName - only thing, that used form context - is getting package name by context.getPackageName(). Package name is the same for Activity.this and Activity.getApplicationContext(), so there is no difference.
I assume you are actually asking about the difference between
startActivity(new Intent(this, ActivityB.class));
and
startActivity(new Intent(getApplicationContext(), ActivityB.class));
There is no difference. Android needs the ComponentName (package name and class). The context is used to determine the package name.