Consider am having 2 applications named first and second. The first applications has activity A the second applications has activity B. Initially Activity A of first application is launched and later second application will be launched from the activity A of the first application. Activity A of the first application has a count down timer. Once the timer hits How can i bring the activity A, being the application second is in visible ?
Thanks in advance.
Are you sure you need them to be different applications? Couldn't they just be activities in the same applications?
That said, you could just use an intent and start your activity that has an intent-filter for that intent?
hi try something like below code
final Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
final ComponentName cn = new ComponentName("com.urbanairship.airmail", "com.urbanairship.airmail.MainListActivity");
intent.setComponent(cn);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity( intent);
Related
Okay, I think I'm missing something here but can't seem to find a way around it :\
So this is my scenario, I have two apps, A and B. A Opens a B with the following intent:
PackageManager pm = getPackageManager();
Intent n = pm.getLaunchIntentForPackage(currAppInfo.getName());
n.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(n);
(currAppInfo is a custom object and getName returns the package name.)
Anyway, B is installing APKs. A receives the package installed broadcast and should be now moved back to front, how ever if I'm starting app A with an intent
Intent serviceIntent = new Intent();
serviceIntent.setClass(context, MainActivity.class);
serviceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(serviceIntent);
Instead of seeing A's mainActivity screen all I see is B's main activity screen.
Why is that? Is it the way I open the apps with the intents or am I missing something more basic here?
My application has two activities A and B. A is the root of the task, and is the one that is launched from the launch icon. B can be started from A.
As well as starting A from the launch icon, it is possible to launch A by clicking on a file in another application, e.g. clicking on an email attachment or a file in Drive. I have done this by adding actions and categories to the intent filter in the manifest file.
I want to make it so that when A is launched from another application, instead of creating a new task I want the existing task to resume in the same state it was in before. This could be activity A or B, wherever the user happened to be before they pressed home.
I have tried all kinds of launch modes and intent flags but nothing seems to work.
Broadcast an intent that's identical to the launcher intent in your manifest:
Intent intent = new Intent(context, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
I don't know why, but this raises the existing task instead of starting a new one. By contrast, a launcher intent obtained the "official" way will actually start a new task:
Intent intent = context.getPackageManager()
.getLaunchIntentForPackage(context.getPackageName());
change launchmode to singletask. and listening onNewIntent()
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
I have a service that once it completes a task it launches an intent to start an activity from my application. Like this:
Intent i = new Intent(MyService.this, MyActivityToBringToFront.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
//i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i);
This service can be running even after my application has been closed. When this is the case and my application is closed I need it to bring the activity to the front of whatever the user is currently doing. So if the user is in a different app I need my activity to pop up in front. Is this possible. None of the flags had any effect. Basically I need it to be like the system phone application. When you get a phone call it always brings the phone to the front. How can I do this? Thanks!
First of all you need to keep context in memory to make intent's.
Secondary you need a mechanism that can re-obtaining your context every 24 hours because usually context stay alive in 24 hours.
After.
Launching Activity from service :
Intent dialogIntent = new Intent(getBaseContext(), myActivity.class);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(dialogIntent);
source : https://stackoverflow.com/a/3607934/2956344
To push activity on top add
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Intent i = new Intent();
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setAction("android.intent.action.VIEW");
i.setComponent(ComponentName.unflattenFromString("com.example.package/com.example.package.activityName"));
startActivity(i);
Intent i = new Intent(MyService.this, MyActivityToBringToFront.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity( i);
And API reference is
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.
For example, consider a task consisting of the activities: A, B, C, D.
If D calls startActivity() with an Intent that resolves to the
component of activity B, then C and D will be finished and B receive
the given Intent, resulting in the stack now being: A, B.
The currently running instance of activity B in the above example will
either receive the new intent you are starting here in its
onNewIntent() method, or be itself finished and restarted with the new
intent. If it has declared its launch mode to be "multiple" (the
default) and you have not set FLAG_ACTIVITY_SINGLE_TOP in the same
intent, then it will be finished and re-created; for all other launch
modes or if FLAG_ACTIVITY_SINGLE_TOP is set then this Intent will be
delivered to the current instance's onNewIntent().
This launch mode can also be used to good effect in conjunction with
FLAG_ACTIVITY_NEW_TASK: if used to start the root activity of a task,
it will bring any currently running instance of that task to the
foreground, and then clear it to its root state. This is especially
useful, for example, when launching an activity from the notification
manager.
launching an activity from a service isn't always a good idea. You may want to use notification for that. If the user clicks on the notification, thats the time you show the Activity.
Is it possible to create two entry points to an application in Android, I mean can I switch the main activity programmatically?
Every exported activity is a potential entry point into your app; a foreign app can start any of them with an intent. (An intent-filter comes with an implicit android:export.) You can however only have one entry point that the launcher will respect. To simulate a second launch-point, either
Provide a completely separate app with the purpose of starting one of your exported activities, or
Give your 'launch' activity the sole purpose of immediately starting one or another activity based on some logic (a saved preference, a phase-of-moon calculation, anything).
check this one below
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setComponent(new ComponentName(packageName,mainActivity));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(intent);
I think you are talking about launching activity decision based on some events, then you need to add a broadcast receiver, like by clicking on app icon on launcher if you want to start Activity1. then add intent filters to this activity Action_MAIN and ACTION_LAUNCHER, if you want to start Activity2 on phone boot up, then add filter to this activity, BOOT_COMPLETED.
If you are talking about to launch other apps from your apps then this can be the code:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setComponent(new ComponentName(packageName,mainActivity));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(intent);