Java Question [Solved] Stuck with onActivityResult

vpires

Member
Licensed User
Longtime User
Hello group.

I'm fighting with the facebook sdk and have hit a (for me) brick wall. The
protected void onActivityResult(int requestCode, int resultCode, Intent data) activity method needs to be overrrided in our activity, so i've put this code :

B4X:
@ShortName("fbGlue")
    @ActivityObject
    public static class fbGlue{
        private IOnActivityResult ion;
        int requestCode;

  
        public void Initialize(BA ba) {
            ion = new IOnActivityResult() {

                @SuppressWarnings("unchecked")
                @Override
                public void ResultArrived(int resultCode, Intent intent) {
                    Log.d("B4A","Result Arrived");
                }
        };
      
        BA pBa =  ba.processBA;
        if (pBa == null) {
            pBa = ba;
            Log.d("B4A","pba was null");
        } else {
            Log.d("B4A","pba was not null");
        }
          
        try {
            pBa.startActivityForResult(ion, null); //<-- passing null instead of an intent
            Log.d("B4A","first pass");
            }
        catch (NullPointerException npe) {
          //required...
          Log.d("B4A","NPE");
        }
        BA.SharedProcessBA sba = pBa.sharedProcessBA;
        try {
            Field f = BA.SharedProcessBA.class.getDeclaredField("onActivityResultCode");
            f.setAccessible(true);
            requestCode = f.getInt(sba) - 1;
            Log.d("B4A","" + requestCode);
            //requestCode holds the value that should be used to send the intent.
          
        } catch (Exception e) {
        throw new RuntimeException(e);
        }
      
        }  
    }

that compiles & runs (I get the facebook login page), but doesn't give the expected results. In the log i see
B4X:
onActivityResult: wi is null

I know that i should use requestCode somewhere, but can't find any place in the facebookSdk to use it. So, does anyone know a workaround for this situation ?

TIA
Nelson
 

vpires

Member
Licensed User
Longtime User
well, that's my problem, it's the code in the facebook sdk that starts the intent.
 

vpires

Member
Licensed User
Longtime User
i'm using a wrapper to easy it's use from here : https://github.com/sromku/android-simple-facebook

i've
1. compiled the facebook sdk and exported as it as jar
2. compiled the android-simple-facebook
3. can use the android-simple-facebook normally in a java app, so i know it's working
4. my wrapper to the android-simple-facebook seems to be working, since it does all the initialization stuff and the b4a

B4X:
Sub Activity_Resume
  f.onResume
  f.Login()
End Sub

shows the facebook login page and i can sign in. The listeners for the login are working.


TIA
 

vpires

Member
Licensed User
Longtime User
for the future in case someone needs it...

B4X:
ion = new IOnActivityResult() {
                @SuppressWarnings("unchecked")
                @Override
                public void ResultArrived(int resultCode, Intent intent) {
                    mSimpleFacebook.onActivityResult(myba.activity, 0xface, resultCode, intent);
                }
        };
        try {
            ba.startActivityForResult(ion, null); //<-- passing null instead of an intent
            }
        catch (NullPointerException npe) {
          //required...
        }
        BA pBa =  ba.processBA;
        BA.SharedProcessBA sba = pBa.sharedProcessBA;
        sba.onActivityResultMap.put(0xface, new WeakReference(ion));

the rest of the wrapper is easy

Tks,
Nelson
 

vpires

Member
Licensed User
Longtime User
Sucess. I can do facebook from B4A.

B4X:
Sub Process_Globals
       
End Sub

Sub Globals
   
    Dim f As fb
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
       
    f.Initialize("fb","531882130230394")
   
    Dim b,b1 As Button
    b.Initialize("bnt")
    b.Text = "Login to FB & Post Photo"
    Activity.AddView(b,0,0,100%x,60dip)
    b1.Initialize("bnt1")
    b1.Text = "Logout from FB"
    Activity.AddView(b1,0,100%y-60dip,100%x,60dip)
   
End Sub
Sub Activity_Resume
    f.onResume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub bnt_Click
    f.Login
End Sub
Sub bnt1_Click
    f.Logout
End Sub
Sub fb_LoginOnLogin
    Dim bmp As FbPhoto
       
    bmp.Initialize
    bmp.Name="B4A App testing"
    bmp.Image = LoadBitmap(File.DirAssets,"users.png")
    f.PublishPhoto(bmp.build,Null)
   
End Sub
Sub fb_PublishPhotoOnComplete(Id As String)
    Log("Photo published with ID=" & Id)
End Sub
Sub fb_PublishPhotoOnException(Exception As String)
    Log(Exception)
End Sub
Sub fb_PublishPhotoOnFail(Reason As String)
    Log(Reason)
End Sub
Sub fb_PublishPhotoOnThinking
    Log("Publish Photo Thinking")
End Sub

gives me this on my fb page.

upload_2014-3-28_16-1-10.png


Now, off to work and fix some computers.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
It was never public. I guess that vpires used the same package name as the BA class. You can access it with this code:
B4X:
BA.SharedProcessBA sba = ba.processBA.sharedProcessBA;
     Field f = sba.getClass().getDeclaredField("onActivityResultMap");
     f.setAccessible(true);
     if (f.get(sba) == null)
       f.set(sba, new HashMap<Integer, WeakReference<IOnActivityResult>>());
     onActivityResultMap = (HashMap<Integer, WeakReference<IOnActivityResult>>) f.get(sba);
 
Top