Java Question Trying to get ionActivityResult to fire

DevBaby

Active Member
Licensed User
Longtime User
I am trying to trap a share result from the Facebook share dialog using steps from the tutorial here and
vpires' solved thread here. He seemed to be able to trap the result by sending
ba.startActivityForResult(ion, null); without ever sending a requestcode or calling another intent for result.

My code is below, i just want to know that the post went through. But I get error: "onActivityResult: wi isnull" in my log after I send a post.

B4X:
package cm.fb.share2;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.ActivityObject;
import anywheresoftware.b4a.BA.DependsOn;
import anywheresoftware.b4a.BA.Permissions;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.IOnActivityResult;

import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.share.Sharer;
import com.facebook.share.model.ShareLinkContent;
import com.facebook.share.widget.ShareDialog;



@ActivityObject
@DependsOn(values={"Facebooksdk410"})
@Permissions(values={"android.permission.INTERNET"})
@ShortName("fbcommand")


public class fbcommand  {

   public static IOnActivityResult ion;
   int requestCode;
   
  CallbackManager callbackManager;
  ShareDialog shareDialog;
   

   public void FbConnect(final BA ba, String Title, String Msg, String LinkUrl, String ImageUrl) {
 
     FacebookSdk.sdkInitialize(BA.applicationContext);
     ShareDialog shareDialog = new ShareDialog(ba.activity);   

     ion = new IOnActivityResult() {
       @Override
       public void ResultArrived(int resultCode, Intent intent) {
       
         Log.d("B4A", "ResultsArrived");
       
         if (resultCode == Activity.RESULT_OK) {
         
           ba.raiseEvent(this, "fbfeed_complete", new Object[] { "OK" });
         
         }
       
       }
     };
   
     BA pba =  ba.processBA;
   
     try {
       pba.startActivityForResult(ion, null); //<-- passing null instead of an intent
     }
     catch (NullPointerException npe) {
       //required...
     }
   
     if (ShareDialog.canShow(ShareLinkContent.class)) {
      ShareLinkContent linkContent = new ShareLinkContent.Builder()
      .setContentTitle(Title)
      .setContentDescription(Msg)
      .setImageUrl(Uri.parse(ImageUrl))
      .setContentUrl(Uri.parse(LinkUrl))
      .build();
         
      shareDialog.show(linkContent);
     }
   } 
}
 
Last edited:

DevBaby

Active Member
Licensed User
Longtime User
See the second post in the tutorial: https://www.b4x.com/android/forum/threads/java-guide-using-onactivityresult.7297/
You are not handling the requestCode at all.

Hi Erel,

I do not know what to do with the requestcode or how it used later (in the code) to get ionActiviyResult to fire. I also don't fully understand post 2 and the order of when to call the sharedialog command in my code above.

When pba.startActivityForResult(ion, null); is called, how does the code know (or grab) the request code if nothing else has been executed to send the request code? Or does pba.startActivityForResult(ion, null) start the ion listener and it receives anything that gets sent and returns to the activity after pba.startActivityForResult(ion, null) was executed.

Also Erel, the author in this thread did not know what to do with the request_code and seemingly did not use it in his solution...however, i am not sure how he got his code to work in relation to what I am trying to do
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
I'm not familiar with this SDK so I don't know how exactly you are expected to call it. The code in post 2 in the tutorial shows you how to get the request code that later should be sent with Activity.startActivityForResult. Without this code B4A will not be able to connect between the result and the IOnActivityResult object.
 

DevBaby

Active Member
Licensed User
Longtime User
I'm not familiar with this SDK so I don't know how exactly you are expected to call it. The code in post 2 in the tutorial shows you how to get the request code that later should be sent with Activity.startActivityForResult. Without this code B4A will not be able to connect between the result and the IOnActivityResult object.

Hi Erel,

The expected call in the sdk is the Sharedialog.show(linkcontent), there is no explicit intent call (per se) unless it is embedded deeper in their sdk, but the user is just exposed to Sharedialog.show(linkcontent). This calls launches the facebook dialog for the user to post a message, the dialog is then closed.

My question is do I call this before or after the code in [post 2] of your "java-guide-using-onactivityresult" tutorial. I don't understand the order of when things should be called.

If the code in [post 2] of your tutorial gets the request_code, what sends the request_code for it to grab?

Are the steps

1) Call Sharedialog.show(linkcontent)
2) Call pba.startActivityForResult(ion, null);
3) Get the request code (post 2 of your tutorial)
4) Use request_code

Is this the order?



 
Last edited:

DevBaby

Active Member
Licensed User
Longtime User
Why do you think that you need to use startActivityForResult at all?

Hi Erel,

In short, to grab the post successful response coming back from the facebook dialog activity to my app (activity).

Older facebook sdk's provided a listener that I used, but the newer sdk just provides the sharedialog...no listener. I can get the sharedialog to both launch and post successfully, but I need to know that the post went through so that I can reward points in my gaming app (the incentive for users to post their game score on facebook). The player gets the rewarded points, and I get the advertisement on their timeline.

I believe that the intent is sent from inside the library (I can't call it directly...Sharedialog is invoking embedded code that calls it), which I believe puts me at [post 2] in your tutorial.

Also, I guess I am confused about the request_code overall. I don't see in [post 2] how the request_code is used afterwards given that [post 2] assumes that you can't call the intent directly.

I have studied this to get a better understanding of intents and results etc.
 

DevBaby

Active Member
Licensed User
Longtime User
The requestCode is expected to be passed to the library. This is different than this case. Here the response is probably handled by the library as well.


Hi Erel,

I believe I can access both the intent and the request code from the sdk.

Do I use just use the
startActivityForResult(IOnActivityResult iOnActivityResult, Intentintent) or do I have to use activity.startActivityForResult with the intent and request code as argument?

How would I use activity.startactivityforresult(Intent, request_code) to make sure that the ion activityforresult is fired given that I am not using ba.startactivityforresult(ion, intent)?

Does ba.startActivityForResult automatically grab the request_code given that it has the intent?
 
Last edited:
Top