-1

I am developing an android app where i am creating a shortcut from HomeActivity and from that shortcut i move back to HomeActivity.

My problem is i want to send some url in shortcut and want to get back that url when user come from shortcut to HomeAtivity.

Here's code :

HomeActivity

private void addShortcut() {

    Intent shortcutIntent = new Intent(getApplicationContext(),HomeActivity.class);
    shortcutIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
    shortcutIntent.putExtra("shortcutKey", "www.myapi.com/fromshortcut");
    shortcutIntent.setAction(Intent.ACTION_MAIN);

    Intent addIntent = new Intent();
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, AppConstants.shortcutTitle);
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
            Intent.ShortcutIconResource.fromContext(getApplicationContext(),R.drawable.ic_launcher));
    addIntent.putExtra("duplicate", false);
    addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");

    getApplicationContext().sendBroadcast(addIntent);

    Toast.makeText(getApplicationContext(), AppConstants.shortcutDone, 40).show();

Shortcut is created successfully. Now i click on Shortcut and come back to HomeActivity and getting Intent but with null pointer exception...

String  shortcutUrl = getIntent().getStringExtra("shortcutKey");

    if(shortcutUrl.equals("")){

        setupWebView("http://stackoverflow.com");

    }
    else{

        Log.e("shortcut was created", "url from shortcut");
        setupWebView(shortcutUrl);

    }

So what should i do to get that data back.Thanks in advance and ready to vote up for right answer.

Elltz
  • 10,730
  • 4
  • 31
  • 59
Rohit Goswami
  • 617
  • 5
  • 17

2 Answers2

0

If i got you correctly, you want url as data back to your home Activity from your shortcut if yes then Start your Activity for result with method startActivityForResult().

Below callback url will give you result back as Intent.

protected void onActivityResult(int requestCode, int resultCode, Intent data)          {

}
Elltz
  • 10,730
  • 4
  • 31
  • 59
kj007
  • 6,073
  • 4
  • 29
  • 47
0

First of all, when you you launch your app with a click on the home screen android opens your app with a default simple Intent like this in String format

Intent { act=android.intent.action.MAIN 
    cat=[android.intent.category.LAUNCHER] - } // the "-" represent flags

this Intent does not have a Bundle with Key shortcutKey that is why you having a NPE

Your title question and explanation is making me drift left & right

Try these

1: get the Activity's Intent and put your url to it via putExtras() and reCreate() your Activity.

getIntent().putExtra("key", "my url");
this.startActivity(getIntent());
this.finish(); // or instead of calling the last two do Activity.recreate()

the catch here is in your onCreate() you check for Intent.hasExtras(); if true then you have your url which means it has been navigated to it, if not you dont have your url,which means you in by the shortcut so you do the logic (that is if you do not have singleInstance on your HomeActivity)

2: or if you have singleInstance set on your Activity then you can check using Flags check this post especially the question - will guide you on how to check using Flags.

Hope all of that was Lucid and helpful

Community
  • 1
  • 1
Elltz
  • 10,730
  • 4
  • 31
  • 59