Android Question onActivityResult with Inline Java

cambopad

Active Member
Licensed User
Dear all,

Today I tried to access a third party SDK using Inline Java but got stuck with onActivityResult error.
I really don't know where to fix this!

This is the original java code:

B4X:
static final int PLAY_REWARDED_VIDEO = 1;
    public void showadvideo() {

        String publisherId = "32740";
        String profileId = "365";
        String subid1 = "demo_subid1";

        Intent intent = RewardedVideoActivity.getIntentForRewardedVideo(this, publisherId, profileId, subid1);

        startActivityForResult(intent, PLAY_REWARDED_VIDEO);

    }
   


    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        BA.Log("onActivityResult");
        // PLAY_REWARDED_VIDEO was declared earlier to identify intent request
        if (requestCode == PLAY_REWARDED_VIDEO && resultCode == RESULT_OK)
        {
            // user was successfully credited
            Log.d("AdscendMedia_", "User was credited");
        }
        else if (requestCode == PLAY_REWARDED_VIDEO && resultCode == RESULT_CANCELED)
        {
            // user was not credited
            Log.d("AdscendMedia_", "User was not credited");
        }
    }

When I tried to compile the app, B4A showed me the following message:

B4X:
Compiling code.    (0.02s)
Compiling layouts code.    (0.00s)
Generating R file.    (1.04s)
Compiling generated Java code.    Error
javac 1.7.0_79
src\com\adscendmedia\helloadscend1\main.java:529: error: method onActivityResult(int,int,Intent) is already defined in class main
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
                ^
1 error

So it says "error: method onActivityResult(int,int,Intent) is already defined in class main", but I checked my codes and found no method that is named onActivityResult.

Can anyone help me to solve this problem? I have learnt a lot about using Inline Java from helpful people on this forum and I feel very happy :) Please help me fix this issue!
 

Roycefer

Well-Known Member
Licensed User
onActivityResult() is called when controls returns to your Activity after your Activity had started another Activity intending to get a result back from that Activity. That is, A starts B, intending to get a result back from B. When B is completed, control returns to A and A's onActivityResult() method is called. This method is already a standard part of Activities so you can't add another "copy" of that method. I think what you should do is test for the starting Intent when your Activity starts and if it contains info about "PLAY_REWARDED_VIDEO" or whatever, then deal with it accordingly.
 
Upvote 0

cambopad

Active Member
Licensed User
Thanks you @Roycefer! Really appreciate your explaination about onActivityResult.

Now, I can compile by removing the Void onActivityResult from the code, but the main problem is that How can I get the result after the activity B finished?

In this case, I think the requestCode, resultCode ,and data are the things I need. Where is the onActivityResult event?
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
You won't be able to directly manage the onActivityResult() event. Instead, you should test for the Intent that started your Activity.
B4X:
Dim data as Intent = Activity.GetStartingIntent
'do something with data to see if it has for what you're looking...

If your Activity is started because another Activity returned a result to it, that "data" Intent will hold that result and info.
 
Upvote 0

cambopad

Active Member
Licensed User
I try this code in Activity_resume event but I got no data at all. All I got in the log is:

B4X:
(Intent) Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.adscendmedia.helloadscend1/.main }

So I would like to explain my situation.

In activity A: I have a button to show a reward video. Pressing that button will show reward video activity provided by the SDK. The code to show the video is :

B4X:
 public void showadvideo() {

        String publisherId = "32740";
        String profileId = "365";
        String subid1 = "demo_subid1";

        Intent intent = RewardedVideoActivity.getIntentForRewardedVideo(this, publisherId, profileId, subid1);

        startActivityForResult(intent, PLAY_REWARDED_VIDEO);

    }

The video started to show and finish showing.....then when I pressed the Back button on my phone to return to Activity A, I hope to get data from that reward video activity. So I tried to put your code in Activity_resume and log the data, but I got something just like I mentioned above. Did I do something wrong? :(
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Try this and see if any of this information is of any use to you:
B4X:
Dim data As Intent = Activity.GetStartingIntent
Log("data.Action: " & data.Action)
Log("data.Extras: " & data.ExtrasToString)
Log("data.Data: " & data.GetData)
Log("data.Flags: " & data.Flags)
Also see if any of that logged information looks like what the SDK examples say you should be getting.
 
Upvote 0

cambopad

Active Member
Licensed User
I tried it, and none of those info are what I need as mentioned in the SDK.

What I got:

B4X:
data.Action: android.intent.action.MAIN
data.Extras: no extras
data.Data: null
data.Flags: 268435456

So the information I need are:

-requestCode
-resultCode

I thinks this two information will be useful to achieve the task according to the SDK.

For example, I write this in B4A:

B4X:
If requrestCode=1 and resultCode=1 then    ' resultCode=1 means RESULT_OK
   Log("User was credited!")
Else if requestCode=1 and resultCode=0 then   'resultCode=0 means RESULT_CANCELED
   Log("User was not credited!")
End if
 
Upvote 0

cambopad

Active Member
Licensed User
Thanks for the link! I just read it, but the guide contains all java codes that are hard to understand to me! :(
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Looking at that post, this is the basic outline of what your inline Java should look like. You'll have a global variable of type IOnActivityResult. You'll pass it (along with an Intent) to ba.startActivityForResult() (instead of just startActivityForResult()). You'll do that by calling showVideo() with JavaObject.
B4X:
private IOnActivityResult ion;
static final int PLAY_REWARDED_VIDEO = 1;

public void showVideo(String eventName) 
{
  String publisherId = "32740";
  String profileId = "365";
  String subid1 = "demo_subid1";
  Intent i = RewardedVideoActivity.getIntentForRewardedVideo(this, publisherId, profileId, subid1);

  ion = new IOnActivityResult() 
  {
     @SuppressWarnings("unchecked")
     @Override
     public void ResultArrived(int resultCode, Intent intent) 
     {
       boolean ok = false;
       if (resultCode == RESULT_OK) ok = true;
       processBA.raiseEvent2(null, true, eventName + "_videoresults", false, ok);
     }
  };
  ba.startActivityForResult(ion, i);
}
It implements the ResultArrived() method which will raise an event with the following signature in your B4A code.
B4X:
Sub EventName_VideoResult(ok as Boolean)

Obviously this is wildly untested. You'll probably have to study it to figure out how to merge this code with the code from your first post in this thread.
 
Upvote 0
Top