Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot make startActivity() with Chooser ask just once per app

When you do startActivity() with chooser, Android would list all apps entitled to handle your Intent along with options to set this assignment permanent or once-time (on ICS its Always and Just once action button, on 2.x it's acheckbox). However for this code:

public class Redirector {
    public static void showActivityWithChooser( Context context, int chooserLabelTitleId, Intent intent ) {
      try {
        context.startActivity( Intent.createChooser( intent, 
                     context.getResources().getString( chooserLabelTitleId )) );
      } catch( Exception e ) {
        e.printStackTrace();
      }
    }

    public static void viewInExternalApplication( Context context, String url ) {
      Intent intent = new Intent(   Intent.ACTION_VIEW );
      intent.setData( Uri.parse( url ) );
      intent.addFlags( Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET );
      showActivityWithChooser( context, R.string.open_chooser_title, intent );
    }
}

I see no Always | Just once buttons and cannot make my selection permanent. What elementary I overlooked that made Android unable to make user choice persistent?

See the pics: left dialog is what I'd like to see, but right is what I get now (different number of applications in both dialogs is irrelevant):

enter image description here

like image 404
Marcin Orlowski Avatar asked Oct 08 '12 11:10

Marcin Orlowski


1 Answers

For a record - it was over-interpretation type of bug (of mine). The chooser I was using is exactly what can be seen on the image on the right side. And it was showing up all the time because... I was calling it all the time. I incorrectly assumed that chooser offers Always | Just once functionality and would not show up if user tapped "Always" (and will show up if s/he used Just once). But it is wrong. Chooser will always show up because that's its role - to let user choose.

The Always | Just once functionality is different thing -> it is a feature of the Android framework for startActivity() (and startActivityForResult() etc) and it would show up automatically when needed (when there's more than one app, or precisely, more than one matching intent-filter declared in app manifest, that matches certain Intent). It would not show up if you got just one (or if user already choose Always).

So you, as developer do not need to care nor handle the case in any special way. To fix the issue I simply changed my viewInExternalApplication() code to just call startActivity():

try {
  context.startActivity( intent );
} catch (.... )

and let the framework do the rest.

like image 166
Marcin Orlowski Avatar answered Sep 24 '22 14:09

Marcin Orlowski