0

This is very strange - I'm trying to programmatically open another app. I found this link which I followed : Stackoverflow link

So my code is as follows - note it is being run inside a dialog.

Intent intentToResolve = new Intent(Intent.ACTION_MAIN);
intentToResolve.addCategory(Intent.CATEGORY_HOME);
intentToResolve.setPackage("com.android.launcher3");
intentToResolve.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
ResolveInfo ri = getPackageManager().resolveActivity(intentToResolve, 0);
if (ri != null)
{
    Intent intent = new Intent(intentToResolve);
    intent.setClassName(ri.activityInfo.applicationInfo.packageName, ri.activityInfo.name);
    intent.setAction(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    startActivity(intent);
}

This seems to only "close" my app and go to the home screen if I put a breakpoint in the code. If I just let it run with no breakpoint then nothing happens.

I have no idea why it would do this? Any ideas? Thanks.

Edit: I've uploaded a video of it happening to YouTube so you can see exactly what I mean. You'll see the first time I run it, it hits the breakpoint and the device goes to the home screen. The second time I run it I have removed the breakpoint and nothing happens. YouTube link

Community
  • 1
  • 1
Mudders
  • 127
  • 2
  • 12
  • do you mean "close" as "force close" ?? Is your app crashing? – SripadRaj Jul 27 '16 at 13:01
  • No, I mean close as in bring the other app (which happens to be the launcher) to the front. So basically my app goes to the background and the desktop is shown. – Mudders Jul 27 '16 at 13:02

2 Answers2

1

The fact that it works if you set a breakpoint would seem to indicate that there's some sort of timing problem.

You mention that you run this code from inside a dialog, which to me, reinforces the idea that you have a timing issue.

Try running this code from the Activity, after the dialog is closed. I'd be very surprised if that doesn't fix the issue.

I assume that you are using the dialog to let the user pick what to launch. So, instead of attempting to launch the other app from the dialog, communicate that info to it's parent activity, and have the Activity run this code after the dialog is closed.

When you show the dialog, you are doing so from an Activity - the dialog is displayed on top of your activity.

You are probably using a Dialog Builder to build the dialog, and then calling builder.create() to show the dialog.

In the builder code, you probably do something like:

.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int id) {
                   // User clicked OK
               }
           })

In the onClick handler, the second parameter is the id of the item that was clicked. Use this info to decide what you want to launch. I'd suggest a separate method in the Activity to do the launching, and then call that from the onClick handler.

GreyBeardedGeek
  • 29,460
  • 2
  • 47
  • 67
0

First thing is make sure do you have the app which belongs to ri.activityInfo.applicationInfo.packageName packagename. Code looks fine, the problem is the package name and the class you give inside setClassName()

madhan kumar
  • 1,560
  • 2
  • 26
  • 36
  • I used the following code to get the packagename: PackageManager pm; pm = getPackageManager(); List packages = pm.getInstalledApplications(0); – Mudders Jul 27 '16 at 13:11