12

I've created a common re-usable class for the company I work for to create some common interface elements.

The class, takes in a single parameter as in the construct: an application context.

one of the methods, ContentClickableRowWithIcon allows you to pass in an intent to be used as the click action.

heres the full method declaration:

public LinearLayout ContentClickableRowWithIcon(Drawable icon, String title, Intent i, final Boolean chooser)

that last attribute there is used in the onClickEvent to determine whether to invoke a Chooser or just go right into the intent.

public LinearLayout ContentClickableRowWithIcon(Drawable icon, String title, Intent i, final Boolean chooser) {

    LinearLayout ll = new LinearLayout(mContext);

    // ..  LinerLayout construction, has nothing to do with the action

    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // this is apparently getting ignored... (ps: i've tried i.setFlags as well)

    final Intent intent = i;

    ll.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

            if(chooser)
                mContext.startActivity(Intent.createChooser(intent, "Complete With...")); // crashes here with: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
            else
                mContext.startActivity(intent); // this works fine

        }
    });

    return ll;
}

As mentioned in the comments, anytime I dont provide the ability to use a chooser, everything works fine (everything in this list gets a new activity flag, im well aware of this and will cleanup when this issue is figured out)

The moment I do, throws the exception: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

I've run out of ideas...

/// EDIT:: Worth noting, on debug, the flags attribute in the Intent is set to 268435456 with addFlags and 268435456 with setFlags, when it reaches the time to use the intent in the onClick action

RedactedProfile
  • 2,748
  • 6
  • 32
  • 51
  • have u tried `mContext.startActivity(Intent.createChooser(i, "Complete With..."));` ? – ρяσѕρєя K Jan 25 '13 at 20:04
  • 2
    My guess would be that the new Intent returned by createChooser() does not have the FLAG_ACTIVITY_NEW_TASK set. Try getting the intent, setting the flag, then calling startActivity – Clyde Jan 25 '13 at 20:08
  • aside from the fact that i needs to be final'd anyways? When I do that (set in the paramters `final Intent i`, the flags never get set at all, flags = 0 in the debug – RedactedProfile Jan 25 '13 at 20:08
  • just did this: `if(chooser) { Intent intent = i; intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); mContext.startActivity(Intent.createChooser(intent, "Complete With...")); }` in leu, still crashed :\ – RedactedProfile Jan 25 '13 at 20:11
  • 2
    try this: `Intent newIntent = Intent.createChooser(intent, "Complete With..."); newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); mContext.startActivity(newIntent);` – Clyde Jan 25 '13 at 20:13
  • hmm intesting @Clyde as it doesn't crash anymore, come sup with the chooser, but doesn't populate the list.... very interesting ok i.. THINK i can work with this... – RedactedProfile Jan 25 '13 at 20:17

3 Answers3

29

Problem has been fixed, I think this is simply the case of an "order of operation" scenario

heres what allowed this thing to work:

    ll.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {



            if(chooser) {
                Intent intent = Intent.createChooser(i, "Complete With");
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                mContext.startActivity(intent);
            } else
                mContext.startActivity(i);

        }
    });

also added a "final" modifier to the parameter in the method declaration

public LinearLayout ContentClickableRowWithIcon(Drawable icon, String title, final Intent i, final Boolean chooser)
RedactedProfile
  • 2,748
  • 6
  • 32
  • 51
  • 11
    Not "order of operation" exactly but the fact that createChooser returns a new Intent. The flag you set in the original intent is not copied to the new one automatically. – Clyde Jan 25 '13 at 20:49
  • +10 (well, if I could anyway). I've been pulling my hair out over this one. The breakthrough is in understand that there are actually *two* intents involved. I was setting the FLAG_ACTIVITY_NEW_TASK on the wrong intent. – PeteH Jun 17 '13 at 08:39
  • I have exactly the same problem. Good to know the "two intent" stuff. haha – Jinghao Shi Nov 12 '13 at 22:10
  • Any assistance with this: http://stackoverflow.com/questions/37071016/how-to-share-by-clicking-on-an-element-inside-the-card-array-adapter will be appreciated. Thanks. – Si8 May 06 '16 at 17:25
  • Oh I need to set the flag on the intent not the context, as the error message suggests. Good to know.. – Heinzlmaen Jan 23 '20 at 14:36
1

Actually your exception mean that you are using Not Activity Context. it could be called from Application context. Check that you are in Activity context since this is not a service

Yevgen Kulik
  • 5,713
  • 2
  • 22
  • 44
0

I fixed it adding the flag to the Chooser Intent

Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND)
            .setType("text/plain")
            .putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here")
            .putExtra(android.content.Intent.EXTRA_TEXT, url)
            .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

context.startActivity(Intent.createChooser(sharingIntent, "Share with").addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
Kevin Perez
  • 667
  • 5
  • 17