Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Current Activity's Intent - getData returns null

I set up an intent filter in a custom AndroidManifest.xml so that my app would show up in other apps' "Share data' menus (ex. "Share Page" in the Browser app).

I added the following intent filter:

<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <action android:name="android.intent.action.EDIT" />
    <action android:name="android.intent.action.SEND" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="text/*" />
</intent-filter>

I'm trying to access the intent by calling the following code (both in app.js and in the first window's js file):

var currActivity = Titanium.Android.currentActivity;
var myIntent = currActivity.getIntent();
var myData = myIntent.getData();

However getData is returning null. Is there any way to retrieve data from the intent that launches your app's activity? So in my example of the Browser, when I select my app from the "Share Page", I'd like to be able to access the url that the browser passed to my app.

— asked July 4th 2011 by Dan Perlberger
  • android intent
0 Comments

1 Answer

  • I figured out how to access the data. Here's how to do it:

    var currActivity = Titanium.Android.currentActivity;
    passedInText = currActivity.getIntent().getStringExtra(Ti.Android.EXTRA_TEXT);
    
    — answered July 5th 2011 by Dan Perlberger
    permalink
    5 Comments
    • Hi Dan,

      I am struggling on getting this setup in my app. Could you maybe post a full working sample for this part specific?

      Where do you put the code? (I reckon in some event listener on app launch?):

      var currActivity = Titanium.Android.currentActivity;
      passedInText = currActivity.getIntent().getStringExtra(Ti.Android.EXTRA_TEXT);
      

      Would be great if you could point out!

      — commented August 14th 2011 by Roeland P
    • The code above is in my app.js. It's not in an event listener. It's just directly in the app.js module.

      — commented August 14th 2011 by Dan Perlberger
    • Ah cool thanks, quick response :). I see this working if the app is not running, as you would fire it up with the intent.

      But what if your app was already running? Is app.js being 'called' again, and thus your code, whenever the app gains focus while already running, due to the broadcast received?

      — commented August 14th 2011 by Roeland P
    • It works whether the app is running or not. I just tried it.

      — commented August 14th 2011 by Dan Perlberger
    • For a demo of the feature, check out my Quick Post app in the Android marketplace (there's a free trial version). It allows you to share text from various apps and post it to Twitter and/or Facebook.

      — commented August 14th 2011 by Dan Perlberger
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.