Umair
Umair

Reputation: 438

Sharing content on Facebook at onClick in RecyclerView

I am trying to share the app content on facebook in an onClick method inside RecyclerView. Recyclerview works fine and showing data properly before the onclick method.

All the dependencies and other stuff is being done in gradle file etc... The problem i am facing is as soon as the user clicks share button, new intent is generated, I am passing relevant parameters but activity doesn't perform any action.

Below is the code for opening Facebook(HomeActivity) onCreate() method:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FacebookSdk.sdkInitialize(getApplicationContext());
    setContentView(R.layout.home);
    if (savedInstanceState == null) {
        Bundle extras = getIntent().getExtras();
        if(extras == null) {
            imageURL= null;
            position=0;
        } else {
            imageURL= extras.getString("imageURL");
            //position = extras.getInt("position");
        }
    } else {
        imageURL= (String) savedInstanceState.getSerializable("imageURL");
        //position= (int) savedInstanceState.getSerializable("position");

    }
    callbackManager = CallbackManager.Factory.create();

//  loginDataBaseAdapter = new LoginDataBaseAdapter(this);
//  loginDataBaseAdapter = loginDataBaseAdapter.open();

    facebookLogin = (LoginButton) findViewById(R.id.login_button);
    facebookLogin.registerCallback(callbackManager, callback);
}

And this is what I am doing during call back method, it login successfully but crashes afterwards.

 public FacebookCallback<LoginResult> callback = new FacebookCallback<LoginResult>() {

    @Override
    public void onSuccess(LoginResult loginResult) {
       // Toast.makeText(getApplicationContext(),"On facebook",Toast.LENGTH_LONG).show();
        AccessToken accessToken = loginResult.getAccessToken();
        ShareLinkContent content = new ShareLinkContent.Builder()
                .setContentUrl(Uri.parse(imageURL))
                .build();
        ShareDialog.show(HomeActivity.this, content);

    }

    @Override
    public void onCancel() {

    }

    @Override
    public void onError(FacebookException error) {
        Profile profile = Profile.getCurrentProfile();
        textView.setText("Connection Lost ! Pleasr Try Again :" + profile.getName());

    }
};

And this is how onClick() method is written:

@Override
    public void onClick(View v) {


        Intent intent = new Intent(context.getApplicationContext(),HomeActivity.class);
        intent.putExtra("position",getAdapterPosition());
        intent.putExtra("imageURL",uri);
        context.startActivity(intent);
    }

Can somebody guide me through this. I am not very experienced in making apps and sharing content on social apps. Any help would be highly appreciated.

Upvotes: 0

Views: 465

Answers (1)

rajeswari ratala
rajeswari ratala

Reputation: 710

You can directly share on facebook by using

    try {
                Intent intent1 = new Intent();
                intent1.setClassName("com.facebook.katana", "com.facebook.katana.activity.composer.ImplicitShareIntentHandler");
                intent1.setAction(Intent.ACTION_SEND);
                intent1.setType("text/plain");
                intent1.putExtra(Intent.EXTRA_TEXT, "any text");
                startActivity(intent1);
            } catch (Exception e) {
                Intent intent = new Intent(Intent.ACTION_SEND);
                String sharerUrl = "https://www.facebook.com/sharer/sharer.php?u=" + "any constant";
                intent = new Intent(Intent.ACTION_VIEW, Uri.parse(sharerUrl));
                intent.putExtra(Intent.EXTRA_TEXT, "any text");
                startActivity(intent);
            }

i.e. try to share on facebook app. if the app is not installed on device the share will be opened on browser

Upvotes: 1

Related Questions