0

I have an Unity app, that has Android plugin that can launch other applications that installed on my smartphone. Here My Java class:

public class LaunchOtherApp  extends Activity
{
    public static Activity mainActivity;

    protected static final String LOGTAG = "MyApp";

    private Activity currentActivity;  
    private Intent i;
   

    private static final LaunchOtherApp ourInstance = new LaunchOtherApp();
    public static LaunchOtherApp getInstance() {
        return ourInstance;
    }

    public LaunchOtherApp(){
        Log.i(LOGTAG,"Created LaunchOtherApp");

    }

    public void Launch( final String pack)
    {
        mainActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Intent intent = new Intent();
                intent.setPackage(pack);
                currentActivity = UnityPlayer.currentActivity;
                Context context = currentActivity.getApplicationContext();
                PackageManager pm = context.getPackageManager();
                List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
                Collections.sort(resolveInfos, new ResolveInfo.DisplayNameComparator(pm));

                if (resolveInfos.size() > 0)
                {
                    try
                    {


                        ResolveInfo launchable = resolveInfos.get(0);
                        ActivityInfo activity = launchable.activityInfo;
                        ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
                        i = new Intent(Intent.ACTION_MAIN);
                        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                        i.setComponent(name);

             
                        context.startActivity(i);
                    }
                    catch (SecurityException e)
                    {
                        intent = getPackageManager().getLaunchIntentForPackage(pack);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                        intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
                        intent.addCategory(Intent.CATEGORY_LAUNCHER);


                        startActivity(intent);

                    }
                }
            }
        });
    }
    @Override
    public void onBackPressed() {
     finish();
    }


Launch - it is method that starting when I click on button in my Unity app... pack variable - it is variable that my Java method receive, and in this variable instantiate Application id. For Example I launched Youtube from my app, and when I pressing Back on my phone, I want to close Youtube, and back to my Unity app... I think I must use onBackPressed method that start when I press Back, but what I must write in this method?
finish(); doesn't help me(( Please, help... Thank you in advance!

Hope1_
  • 15
  • 5

1 Answers1

0

Actually, YouTube app is a separate app (might be installed) and you cannot modify that app's behavior. On your button press, Launch method is called and assume YouTube app has started. With the intent:

If you use Intent.FLAG_ACTIVITY_NEW_TASK

YouTube app will start in a new task than yours and pressing back will not return to your app.

If you do not use Intent.FLAG_ACTIVITY_NEW_TASK

The new intent (here YouTube) will be launched in the task of calling activity (your app/game) by default and calling activity will go background. So, pressing back will again return to your app/game as you desired. Because back button press always pops from the backstack of the current task.

So, in the try block of your Launch method, removing the following line should help.

i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);

For more information about task and back stack, here is the official doc.

Edit

I missed that you have used application context. But, you have to launch from your calling activity context, otherwise you must use this flag. So, I have modified your Launch method and put it here, just replace your Launch method with following:

/* Method to launch an app with its package name */
public void Launch( final String pack)
{
    mainActivity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            
            // Using activity context instead of application context
            currentActivity = UnityPlayer.currentActivity;
            Context context = currentActivity;
            PackageManager pm = context.getPackageManager();
            
            // Similar code to launch from package name
            Intent intent = pm.getLaunchIntentForPackage(pack);
            if (intent == null) {
                // The activity cannot be found or the package name cannot be recognized
                // Show some message or do something
                Toast.makeText(context, "Cannot launch...", Toast.LENGTH_SHORT).show();
            } else {
                context.startActivity(intent);
            }
        }
    });
}
Ananta Raha
  • 1,011
  • 5
  • 14
  • no, it doesn't help me: my app crash now when I try to launch other application xD. In my logcate following error: ```Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?``` – Hope1_ Jul 03 '21 at 15:30
  • Hi, I've read your code thoroughly. I did similar work from my app months ago, so I tested it again and posted your whole `Launch` method. Please, see my updated answer. Hope, this helps. – Ananta Raha Jul 04 '21 at 09:53
  • Hello Ananta Raha! First of all - thank You for Your help! My application crash when I use this line: ```startActivity(intent);```, so I changed it to ```context.startActivity(intent);```. I need to make sure that this activity does not rolle up, but turns off, so as not to occupy the memory of the smartphone ... – Hope1_ Jul 04 '21 at 11:38
  • Yes, I did a bit mistake again, context.startActivity(intent). Did it solve your problem? And I think using activity context should not cause memory leak in this situation, because you are not keeping a reference to it. – Ananta Raha Jul 04 '21 at 12:36
  • No, it didn't solve my problem... application, that I launched doesn't close and and continues to run in the background and take up the memory of the smartphone... – Hope1_ Jul 04 '21 at 12:42
  • When you launch another app, and exits from that app pressing back, its process mightstill stay in memory, and you have no right to kill that app's process but the android system decides when to terminate it (for example, when low on memory). You need not worry about this. I just thought you wanted the behavior "pressing back will again return to your app/game" and so I answered. Did I get you right? – Ananta Raha Jul 04 '21 at 13:16
  • oh, thank You, Ananta Raha! it is a pity that I can not close the third party application, but you say that need not worry about this... I remember that user can turn off every app by hand... Can I simulate this action from my application? – Hope1_ Jul 04 '21 at 13:36
  • No, there is no way to simulate this (managing process or its lifecycle) since it is handled by the system. However, you can modify some task-behavior of your own app if necessary at all. Here is an answer https://stackoverflow.com/a/10597017/16146250 – Ananta Raha Jul 04 '21 at 14:00
  • ok, I now understand! Thank You for Your answer and for Your comments! I wish You good luck, Ananta Raha! – Hope1_ Jul 04 '21 at 14:59