Mudasar Rauf
Mudasar Rauf

Reputation: 552

Android V7 ShareActionProvider Share Button not working

I am facing a weird problem with ShareActionProvider. Every thing is linked properly. The share button is there but it don't work. I do see an event on logcat but on phone nothing comes up.

menu.xml

<xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/action_share"
    android:title="@string/action_share"
    app:showAsAction="always"
    app:actionProviderClass="android.support.v7.widget.ShareActionProvider"
/>

Fragment menu init code

import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.widget.ShareActionProvider;

public DetailsFragment() {
    setHasOptionsMenu(true);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    // Inflate the menu; this adds items to the action bar if it is present.
    inflater.inflate(R.menu.detailsfragment_menu, menu);
    MenuItem menuItem = menu.findItem(R.id.action_share);
    mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(menuItem);
    // If onLoadFinished happens before this, we can go ahead and set the share intent now.
    if (YOUTUBE_KEY != null) {
        mShareActionProvider.setShareIntent(createShareForecastIntent());
    }
}

//Share intent code

 private Intent createShareForecastIntent() {
        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
        shareIntent.setType("text/plain");
        Uri videoLocation = Uri.parse(YOUTUBE_API).buildUpon()
                .appendQueryParameter("v", YOUTUBE_KEY)
                .build();
        shareIntent.putExtra(Intent.EXTRA_TEXT, MOVIES_SHARE_HASHTAG);
        shareIntent.setData(videoLocation);

        return shareIntent;
    }

Upvotes: 2

Views: 828

Answers (2)

Pavneet_Singh
Pavneet_Singh

Reputation: 37404

With ShareActionProvider it required to set the content as soon as it is initialize so do this inside you onCreateOptionMenu with dummy content to share and after initialization get the reference of ShareProvider into a Global ShareActionProvider Object i.e globalShareActionProvider

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.detailfragment, menu);
    MenuItem menuItem = menu.findItem(R.id.action_share);

    // Get the provider and hold onto it to set/change the share intent
    ShareActionProvider mShareActionProvider =
            (ShareActionProvider) MenuItemCompat.getActionProvider(menuItem);

    // Attach an intent ot this ShareActionProvider.
           if (mShareActionProvider != null) {
        mShareActionProvider.setShareIntent(createShareForecastIntent());
        globalShareActionProvider=mShareActionProvider;
    } else {
        Log.d(LOG_TAG, "Share Action Provider is null?");
    }
}

after this, call setShareIntent to create new intent with updated values

like i used it inside my onLoaderFinish function

        shareActionString= String.format("%s - %s - %s/%s",
                mdateView.getText(), mNewShareContent, hour, minutes);
      globalShareActionProvider.setShareIntent(createShareForecastIntent());

my createShareForecastIntent()

private Intent createShareForecastIntent() {
    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    shareIntent.setType("text/plain");
    shareIntent.putExtra(Intent.EXTRA_TEXT, shareActionString
            + SHARE_HASHTAG);
    return shareIntent;
}

Upvotes: 1

user2494388
user2494388

Reputation: 1

Try this

public static final String VIDEOS_URL = "http://www.youtube.com/watch?v=";  

public String getFullUrl(){
    return this.VIDEOS_URL + getKey();
}

private Intent createShareVideoIntent() {
    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
    shareIntent.setType("text/plain");
    shareIntent.putExtra(Intent.EXTRA_TEXT, getFullUrl());
    return shareIntent;
}

And if you are usign cursorLoader you have to check if YOUTUBE_KEY is distinct of null in onLoadFinished too.

Upvotes: 0

Related Questions