1

After starting default calculator and hitting back navigation, the user is returned to a different activity rather than the calling activity. When the user clicks the calculator button, the devices default calculator opens. Upon invoking onBackPressed, the user is returned to a different activity within the app that they should not be going to. How can I make sure that the user is returned to the calling activity? Here's my code and what I have tried:

    public static void openByName(Context context, String name){
        ArrayList<HashMap<String,Object>> items =new ArrayList<HashMap<String,Object>>();

        final PackageManager pm = context.getPackageManager();
        List<PackageInfo> packs = pm.getInstalledPackages(0);
        for (PackageInfo pi : packs) {
            if( pi.packageName.toLowerCase().contains(name)){
                HashMap<String, Object> map = new HashMap<String, Object>();
                map.put("appName", pi.applicationInfo.loadLabel(pm));
                map.put("packageName", pi.packageName);
                items.add(map);
            }
        }

        if(items.size()>=1){
            String packageName = (String) items.get(0).get("packageName");
            Log.d(TAG, "PackageName: " + packageName);
            Intent i = pm.getLaunchIntentForPackage(packageName);
            if (i != null)
                context.startActivity(i);
        }
        else{
            // Application not found
            Toaster.make(context, name + " not found");
        }
    }

After different method attempts, I had to use this way because it otherwise wouldn't work on some devices.

Steve C.
  • 1,333
  • 3
  • 19
  • 50
  • http://stackoverflow.com/questions/18243515/android-going-back-to-previous-activity-with-different-intent-value/18243541#18243541? – codeMagic Mar 11 '17 at 01:05
  • I saw that, that wouldn't work as I'm not calling the intent expecting a result. Given the code I have, if I try to change 'context.startActivity()' I only have options to define a single intent rather than like with a normal intent where I would define the calling class and the target class. And there in lies my problem :/ – Steve C. Mar 11 '17 at 01:10
  • 1
    Did you call startActivity with finish() ?? – R.R.M Mar 11 '17 at 08:11
  • @R.R.M thank you for pointing that out. Can't believe I missed that – Steve C. Mar 11 '17 at 09:43
  • No need of thanks.. :) – R.R.M Mar 11 '17 at 09:48

0 Answers0