Java Question [Solved] Stuck with onActivityResult

Discussion in 'Libraries developers questions' started by vpires, Mar 27, 2014.

  1. vpires

    vpires Member Licensed 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 :

    Code:
    @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
    Code:
    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
     
  2. Erel

    Erel Administrator Staff Member Licensed User

  3. vpires

    vpires Member Licensed User

    well, that's my problem, it's the code in the facebook sdk that starts the intent.
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    Where are facebook sdk instructions?
     
  5. vpires

    vpires Member Licensed 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

    Code:
    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
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    I don't think that it will work in this case.
     
  7. vpires

    vpires Member Licensed User

    for the future in case someone needs it...

    Code:
    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
     
  8. vpires

    vpires Member Licensed User

    Sucess. I can do facebook from B4A.

    Code:
    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 likes this.
  9. alwaysbusy

    alwaysbusy Expert Licensed User

    I guess this trick (of @vpires ) does not work anymore as sba.onActivityResultMap is not visible anymore. I have to start an activityforresult, using a fixed request code (0xce8b in my case). Any workarounds?
     
    Last edited: Feb 28, 2017
  10. Erel

    Erel Administrator Staff Member Licensed 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:
    Code:
    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);
     
    alwaysbusy likes this.
  11. alwaysbusy

    alwaysbusy Expert Licensed User

    That did the trick! I was getting close, just didn't create the new HashMap stuff so I got a null pointer exception :confused:
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice