Search code examples
androidonactivityresult

get name of the app the user chose from app list (android)


making an android app, i have this intent setup to let the user choose from a list of all apps and it works ok.

            Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
            mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);            
            Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
            pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
            startActivityForResult(pickIntent, 0);

i have also added the an onActivityResult() method without writing any code in it. What code do i need in the onActivityResult() method to get the package name of the app the user chose?

(or any other information with which i can launch the chosen app)


Solution

  • @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == 0 && resultCode == Activity.RESULT_OK && data != null) {
            ComponentName componentName = data.getComponent();
            final String packageName = componentName.getPackageName();
            final String activityName = componentName.getClassName();
        }
    }
    

    or you can just startActivity(data); so you can launch which app that user choosed.