when the user clicks on the icon in the app drawer to start my application, my app opens in the state it was last in.
I have a broadcast receiver, and when I receive a certain intent I want to have the same effect.
At the moment I use
Intent launchIntent = mContext.getPackageManager().getLaunchIntentForPackage("xx.xx.xx"); //My package name
context.startActivity(launchIntent);
But this always opens the mainactivity of my application.
How can I fix this? thx
Related
I got an app which can receive an event (which is basically a message from a service in the background), and when it does, I am invoking this code:
Intent dialogIntent = new Intent(this, MainActivity.class);
dialogIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(dialogIntent);
This code is supposed to return the app to the front so the user can handle the event.
This code works only if the app I'm working on was also the last app the user has been in. The code doesn't work in these scenarios:
1) The user has paused the app, opened another app and then paused it too so the 2nd app is the last in the background - in which case the last app will always open, even though I'm starting my MainActivity. For example, if I've moved my app to the background, opened Android's settings and then an event is received, the Android's settings screen will open instead of my app.
2) The user is inside another app. In which case, the app won't be brought to the front at all.
How can I made the specific app to be resumed from the background no matter what?
Use launcher category as below:
Intent resumeIntent = new Intent(context, MainActivity.class);
resumeIntent.setAction(Intent.ACTION_MAIN);
resumeIntent.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(resumeIntent);
Also add android:launchMode="singleTop" in the manifest of your activity.
You can add android:launchMode="singleTop" in the manifest for the Activity.
I am having two apps. First app has an activity from which I want to launch an activity from the second app. I am using the following code:
Intent launchIntent = m_context.getPackageManager().getLaunchIntentForPackage(m_packageName);
if (launchIntent != null) {
m_context.startActivity(launchIntent);
}
This code is working very fine to launch the activity from the second app but I want to have the second application without any icon. I am using following code in MainActivity of the second application to remove icon:
PackageManager p = getPackageManager();
//Removing app icon
ComponentName componentName = new ComponentName(this, com.tools.html2pdf.MainActivity.class);
p.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
This code successfully removes the launcher icon but then activity from my first application is unable to launch the activity from second app.
Can any one help me in this regard? I want to launch activity of an app having no icon from activity of another application.
When you disable the component like you have done, that component can't be launched in any way. However, interesting thing is that other components (non-disabled activities) of your second application are still launchable.
So, you can create an alias of your MainActivity in the second application which will be used for your purpose. Let's call alias as MainActivityAlias.
From your first application, call the intent on MainActivity. The code for disabling the component will be executed and nothing will open. However, the icon will be gone because this component is disabled and everything related to this component (i.e icon) is gone too.
Now, call the intent on MainActivityAlias just after above intent in the first application. This is just a copy of MainActivity but it does not have any code for disabling and thus, it is enabled and launchable.
Some Side Notes :
1) Both activities should have an <intent-filter> with android.intent.action.MAIN.
2) Your MainActivity should be the launcher activity and thus should have android.intent.category.LAUNCHER in the manifest.
3) Inside MainActivity, you have to check where the call is coming from. If the call is from the first application, then execute the code to disable icon which you mentioned in the question. If the call is coming from launcher icon, then open MainActivityAlias using intent. You can know where the call is coming from like this.
Note - This is just an idea. I have not tested it.
If you don't want the second app to have an app icon, just remove the <intent-filter> with ACTION=MAIN and CATEGORY=LAUNCHER for the root Activity in the second app. When the app is installed, if there is no <intent-filter> with ACTION=MAIN and CATEGORY=LAUNCHER, there will be no app icon shown.
Your app can still launch the second app, but not with the method you've described, since Android doesn't know which is the "launch" Activity. Assuming you know the package and class name of the Activity you want to launch in the second app, you can launch it like this:
Intent launchIntent = new Intent();
launchIntent.setClassName("second.package.name", "fully.qualified.class.name.of.MainActivity");
// add and Intent flags if necessary here
launchIntent.addFlags(Intent.FLAG_ACTIVITY_...);
startActivity(launchIntent);
I have a problem regarding Android task and intent management.
Scenario
User gets a push with a deep-link into the app.
We show a notification putting the URI into the Intent Data.
User clicks the notification and is taken into the app and redirected to some Feature1Activity described by the deep-link.
User looks around, and backs out of the app.
Later, user opens the app from history (long-press home or multitasking button).
Now the same intent that were used from the notification (with the deep-link in the Intent Data) is used to start the app.
Hence, user is taken into the Feature1Activity again.
Problem:
Starting the app from history (long-press home or multitasking button) does not reset the Task (which it does when launching from app icon).
I understand that starting an app from history is not supposed to reset the task since it is intended to be used as "get-right-back-where-you-were". However, in my case this is an issue since the launch of the app from a notification is a one time thing.
Anyone else encountered this problem? Anyone know any solution?
More in-depth
The intent inside the PendingIntent is built like this:
Intent intent = new Intent (Intent.ActionView);
intent.addFlags (Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags (Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags (Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
intent.setData (Uri.Parse (DEEP_LINK_URL));
I found out about the FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET just this day and really thought that it would git rid of my problem but it made no difference.
There are three activities of interest:
SplashActivity (main launcher & listener of the deep-linking schema -- this activity just redirects either to login or OverviewActivity)
OverviewActivity (authorized user's main activity)
Feature1Activity (any feature that the deep-link is pointing to)
What happens when the user clicks the notification is that the SplashActivity acts as a listener for the schema and converts the deep-link url to two intents to start up OverviewActivity and Feature1Activity using Activity.startActivities (Intent[]).
When I look at the intent from the notification inside SplashActivity it always contain the deep-link in the Data.
One work around
There is a work around, setting some booleanExtra field to the notification intent (for instance "ignoreWhenLaunchedFromHistory" = true) and then check in SplashActivity before redirecting
boolean fromHistory = (getIntent().getFlags() & FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY;
if (fromHistory && getIntent().getBooleanExtra ("ignoreWhenLaunchedFromHistory", false))
// Don't follow deep-link even if it exists
else
// Follow deep-link
Except that it hackish and ugly, can you see any problems with this work around?
EDIT: The work around only works when I am responsible for sending the Intent with the deep-link. Since no external source can know about the "ignoreWhenLaunchedFromHistory" extra.
From what I get, maybe using android:excludeFromRecents="true"on your manifest (as a property for the Activity declaration) might ameliorate the issue?
I've a problem developing an application, I'm building a Widget that can be either on lockscreen or in home page. when is in lockscreen I want that when the user clicks a button the widget prompt for the user login in case of needed. I mean prompt for patter or password or face recognition depending if the user have any of this security enabled.
after the user enter his pattern the widget will run an application (intent).
I've notice that some widgets does prompt for login and other doesn't I haven't found the difference between them.
I think this has to be on the onReceive method but not sure how can I call this "login" method, so far on the onReceive I start the intent
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
i.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
context.getApplicationContext().startActivity(i);
but this still not prompt for login. when the widget is on the lockscreen.
let say that the user interacts with the widget in the Lock screen, the user is not prompted to enter the password, either way the user does enter his pattern|password and after the user "enter" his home screen and the application is started. So the widget does start the intent just don't let the user "know" or login to see the application.
any thoughts on how can I prompt the user to enter his pattern so it enter to the home screen?
[Edit]
The problem occurs when I try to fire the intent within a BroadcastReceiver.
What I do is allow the user interact with the widgetd buttons, depending on what the user has selected I'll open my application adding some extras to the intent.
so on the onReceive method of my BroadcastReceiver I try to fire:
Intent i = new Intent(context, MyActivity.class);
i.putExtra(WidgetUtils.TAG_CURRENT_SELECTION, StringIGotFromInteraction);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.getApplicationContext().startActivity(i);
this way the intent don't prompt for the "unblock".
If in the onUpdate I cast the pending intent to any button when the user clicks that button does prompt for the unlock this way:
views.setOnClickPendingIntent(R.id.aButton, buildAPendingIntent(context));
but I need to know what has the user selected to open and Intent in case I open any.
and my contex.startActivity Approach works fine when the widget is on home screen just not on loockscreen.
Any thoughts how can I make this happen.
I couldn't find the answer what I end up doing was create a dynamic button and when the widget is updated (by the other buttons) I assign the PendingIntent to this button.
So when user clicks this dynamic button the widget attempt to open the intent and is there when the user is prompted for the password.
I think this is not the best solution seance we make the user make an extra click on the widget.
In my app I have a splash activity which is also the launcher, a main menu activity, a sub menu activity, and a description activity.
When I start my app the splash.java loads and then starts the main_menu intent.
The main_menu intent displays a button; this button then starts the sub_menu intent which displays a different button that starts the description intent.
However, when I click that first button on the "main_menu" android displays a prompt asking to "complete action with" and displays two duplicate names of my app, one works and the other does not.
Also, I only get the prompt on the main_menu activity and not on the sub_menu activity.
Any theories/ideas would be greatly appreciated.
If you have several intent-filters for activities that can process a fired intent then you'll receive this kind of dialog. This means that the fired intent can be processed by several activities (for instance, you can have several image viewers installed in the system and, thus, when you fire an intent to open an image this dialog will appear and ask you which activity you want to open to process this intent)