0

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.

Darush
  • 11,403
  • 9
  • 62
  • 60
Salman Nazir
  • 2,759
  • 2
  • 28
  • 42

2 Answers2

0

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.

Sachin Aggarwal
  • 1,095
  • 8
  • 17
0

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);
David Wasser
  • 93,459
  • 16
  • 209
  • 274