2

I'm implementing Chrome Custom tabs (using latest version 23.3.0) on our Android app. Most recent versions of chrome tabs allow you to add button on a bottom toolbar by using "builder.addToolbarItem()" method (other customizable stuff according to this stack overflow answer. Now I'm facing a problem when adding action intents for my bottom toolbar buttons. I set two different action intents for each of the toolbar items I add. But when opening the chrome custom tab, and clicking on any of the toolbar items I've added, the same intent is launched. The intent launched always corresponds to the intent set for the the first toolbar item added .

Here is the code I use to build the custom tabs when clicking on a item of a recycler view.

 protected void openCustomTab(Listing listing, int position) {
    CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
    builder.setToolbarColor(ContextCompat.getColor(this, R.color.primary));
    builder.setStartAnimations(this, R.anim.slide_in_right, R.anim.slide_out_left);
    builder.setExitAnimations(this, R.anim.slide_in_left, R.anim.slide_out_right);
    builder.setCloseButtonIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_arrow_back));

    addShareAction(listing, builder);
    setBottomToolbar(listing, position, builder);
    CustomTabActivityHelper.openCustomTab(
            this, builder.build(), Uri.parse(listing.getListingURL()));
}

private void setBottomToolbar(Listing listing, int position, CustomTabsIntent.Builder builder) {
    builder.setSecondaryToolbarColor(ContextCompat.getColor(this, R.color.color_bottom_toolbar));
    addFavoriteButton(listing, position, builder);
    addReportButton(position, builder);
}

private void addShareAction(Listing listing, CustomTabsIntent.Builder builder) {
    Bitmap iconShare = BitmapFactory.decodeResource(getResources(),
            R.drawable.ic_share_custom_tab);
    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.setType("text/plain");
    shareIntent.putExtra(Intent.EXTRA_SUBJECT, getResources().getString(R.string.share_item_message));
    shareIntent.putExtra(Intent.EXTRA_TEXT, listing.getShareURL());
    PendingIntent pi = PendingIntent.getActivity(this, 0, shareIntent, 0);
    builder.setActionButton(iconShare, getString(R.string.share_item_message), pi, true);
}

private void addReportButton(int position, CustomTabsIntent.Builder builder) {
    Bitmap reportIcon = BitmapFactory.decodeResource(getResources(),
            R.drawable.detail_bottom_hide);
    Bundle menuBundle = ActivityOptions.makeCustomAnimation(this, R.anim.slide_in_left,
            R.anim.slide_out_right).toBundle();
                   Intent reportIntent = createDetailActionIntent(position, ViewsConstants.REPORT);
    PendingIntent pi = PendingIntent.getActivity(this, ACTION_REPORT, reportIntent, 0, menuBundle);

    builder.addToolbarItem(2, reportIcon, getString(R.string.report_listing_dialog_title), pi);
}

private void addFavoriteButton(Listing listing, int position, CustomTabsIntent.Builder builder) {
    Bitmap favIcon;
    if (listing.getIsFavorite()) {
        favIcon = BitmapFactory.decodeResource(getResources(),
                R.drawable.detailbottom_fav);
    } else {
        favIcon = BitmapFactory.decodeResource(getResources(),
                R.drawable.detailbottom_nofav);
    }
    Bundle menuBundle = ActivityOptions.makeCustomAnimation(this, android.R.anim.slide_in_left,
            android.R.anim.slide_out_right).toBundle();
    Intent favIntent = createDetailActionIntent(position, ViewsConstants.FAVORITE);
    PendingIntent pi = PendingIntent.getActivity(this, 0,favIntent, 0, menuBundle);
    builder.addToolbarItem(1, favIcon, getString(R.string.add_favorite), pi);
}

private Intent createDetailActionIntent(int position, String action) {
    Intent actionIntent = new Intent(getApplicationContext(), ListingActivity.class);
    actionIntent.putExtra(ViewsConstants.SEARCH_PARAMETERS, getListingPresenter().getSearchParameters());
    actionIntent.putExtra(ViewsConstants.POSITION, position);
    actionIntent.putExtra(ViewsConstants.DETAIL_ACTION, action);
    return actionIntent;
}

So after running this code a get a chrome custom tab with a bottom toolbar and two buttons favorite and report. Clicking on any of the buttons will always launch the favorite action.. I've have make sure I'm passing the values correctly to the intents by debugging the code several times. I don't know what I'm missing here. I'm starting to think it could be a bug on Chrome custom tabs, but probably there is something I'm missing. Any help would be highly appreciated. Thanks in advance.

EDITED

I've edited the code base on suggestions given and now it works properly regarding the action to perform for each button. Than you! But I still have one more problem, regarding the position. I'm setting the position of the item selected on the intent, but when opening chrome tab, clicking on any action button and comming back to the activity, the intent only has the action set but the position is lost. I can't understand why? I am setting all the intent values in method createDetailActionIntent() shown in the code above.

Any ideas on why position is lost when comming back from the chrome custom tab to the activity and retrieving intent extras???

This post helped me with this last issue I was having.

Thanks all who contributed to solving this problem!

Community
  • 1
  • 1
JorgeMuci
  • 618
  • 8
  • 19

2 Answers2

2

The same intent is launched because you create two PendingIntent with the same Intent action and the same request code.

To resolve your issue change the request code of PendingIntent.getActivity in addFavoriteButton:

private void addFavoriteButton(Listing listing, int position, CustomTabsIntent.Builder builder) {
    Bitmap favIcon;
    if (listing.getIsFavorite()) {
        favIcon = BitmapFactory.decodeResource(getResources(),
                R.drawable.detailbottom_fav);
    } else {
        favIcon = BitmapFactory.decodeResource(getResources(),
                R.drawable.detailbottom_nofav);
    }
    Bundle menuBundle = ActivityOptions.makeCustomAnimation(this, android.R.anim.slide_in_left,
            android.R.anim.slide_out_right).toBundle();
    Intent favIntent = createDetailActionIntent(position, ViewsConstants.FAVORITE);
    PendingIntent pi = PendingIntent.getActivity(this, 1,favIntent, 0, menuBundle);
    builder.addToolbarItem(1, favIcon, getString(R.string.add_favorite), pi);
}

If you want to know more about how PendingIntent works you can read this StackOveflow answer.

Community
  • 1
  • 1
Mattia Maestrini
  • 30,486
  • 15
  • 81
  • 89
  • Thanks for the fast reply, I have edited mi answer with the suggestions. I still have a problem though, regarding the position. I've explained in more detail in my edit. Could you please take a look at it? – JorgeMuci Apr 15 '16 at 10:25
1

You need to pass a different requestCode to the PendingIntent.getActivity call, otherwise the PendingIntent is overwritten. Check the PendingIntent docs for more detail.

The Github demo for Custom Tabs has code that implements this the right way.

Your code should look like the following:

private static final int ACTION_CODE_REPORT = 1;
private static final int ACTION_CODE_SHARE = 2;

private void addShareAction(Listing listing, CustomTabsIntent.Builder builder) {
    Bitmap iconShare = BitmapFactory.decodeResource(getResources(),
            R.drawable.ic_share_custom_tab);
    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.setType("text/plain");
    shareIntent.putExtra(Intent.EXTRA_SUBJECT, getResources().getString(R.string.share_item_message));
    shareIntent.putExtra(Intent.EXTRA_TEXT, listing.getShareURL());
    PendingIntent pi = PendingIntent.getActivity(this, ACTION_CODE_SHARE, shareIntent, 0);
    builder.setActionButton(iconShare, getString(R.string.share_item_message), pi, true);
}

private void addReportButton(int position, CustomTabsIntent.Builder builder) {
    Bitmap reportIcon = BitmapFactory.decodeResource(getResources(),
            R.drawable.detail_bottom_hide);
    Bundle menuBundle = ActivityOptions.makeCustomAnimation(this, R.anim.slide_in_left,
            R.anim.slide_out_right).toBundle();
    PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), ACTION_CODE_REPORT,
            createDetailActionIntent(position, ViewsConstants.REPORT), 0, menuBundle);
    builder.addToolbarItem(2, reportIcon, getString(R.string.report_listing_dialog_title), pi);
}
andreban
  • 3,575
  • 1
  • 16
  • 40
  • Thanks for the fast reply, I have edited mi answer with the suggestions. I still have a problem though, regarding the position. I've explained in more detail in my edit. Could you please take a look at it? – JorgeMuci Apr 15 '16 at 10:25
  • 1
    Take a look at the following post. Maybe it helps: http://stackoverflow.com/questions/3127957/why-the-pendingintent-doesnt-send-back-my-custom-extras-setup-for-the-intent – andreban Apr 15 '16 at 13:31
  • Awesome, just what I needed. Thanks – JorgeMuci Apr 16 '16 at 11:30