What is needed in the onActivityResult method after a purchase flow has been launched?
From the Trivial Drive sample:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (mHelper == null) return;
// Pass on the activity result to the helper for handling
if (!mHelper.handleActivityResult(requestCode, resultCode, data)) {
// not handled, so handle it ourselves (here's where you'd
// perform any handling of activity results not related to in-app
// billing...
super.onActivityResult(requestCode, resultCode, data);
}
else {
Log.d(TAG, "onActivityResult handled by IABUtil.");
}
}
"here's where you'd perform any handling of activity results not related to in-app billing"
Does it mean you need to update the user's inventory or display an alert box? If so, I do this already in the OnConsumeFinishedListener. I have tested my code leaving the onActivityResult method as above and it seems fine. Could this potentially cause any problems?
Or does it mean I must manually call the consume method for the SKU purchased?
Your code is fine if you have not to handle other results in your activity.
Imagine an activity which e.g. start other activitys with startActivityForResult().
That's the place to handle those 'not related to in-app billing' results.
But then you should change the code to something like:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Pass on the activity result to the helper for handling
if (mHelper==null || !mHelper.handleActivityResult(requestCode, resultCode, data)) {
// not handled, so handle it ourselves (here's where you'd
// perform any handling of activity results not related to in-app
// billing...
}
else {
Log.d(TAG, "onActivityResult handled by IABUtil.");
}
super.onActivityResult(requestCode, resultCode, data);
}
Related
I have to throw in the towel on this. I am trying to use Image Cropper: Arthur Hub in a Fragment and I keep getting this
error: onActivityResult(int,int,Intent) in ProfileFragment cannot
override onActivityResult(int,int,Intent) in Fragment attempting to
assign weaker access privileges; was public
Here is the imageCropper function in the fragment:
private void ImagePicker() {
CropImage.activity(mainImageUri)
.setGuidelines(CropImageView.Guidelines.ON)
.setAspectRatio(1, 1)
.start(startActivityForResult();,this);
}
And here is the onActivityResult in the same fragment I am using to obtain the image:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == Activity.RESULT_OK) {
mainImageUri = result.getUri();
profileImage.setImageURI(mainImageUri);
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
Exception error = result.getError();
}
}
}
I had this implemented previously in an activity and it worked fine. As soon as I adjusted it to work in a Fragment, I cannot proceed.
Please help! Also I am a relatively new developer so please be a bit more descriptive in your explanation. Thanks!
In fragment overrides onActivityResult like this:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) { //note: public, not protected.
super.onActivityResult(requestCode, resultCode, data);
}
and in fragment call startActivityForResult method.
I am creating an app that has the LinkedIn login. I am following this documentation. But when I click on the login button, the app redirected to the LinkedIn app and asking for login. When successful login it redirected to my app. But nothing happens. Nothing happens on onActivityResult also.
Below is my code .Login implemented on fragment
LISessionManager.getInstance(getActivity()).init(getActivity(), buildScope(), new AuthListener() {
#Override
public void onAuthSuccess() {
getLinkedInProfile();
Toast.makeText(getApplicationContext(), "success" , Toast.LENGTH_LONG).show();
}
#Override
public void onAuthError(LIAuthError error) {
Toast.makeText(getApplicationContext(), "failed " + error.toString(), Toast.LENGTH_LONG).show();
}
}, true);
//
private static Scope buildScope() {
return Scope.build(Scope.R_BASICPROFILE, Scope.R_EMAILADDRESS);
}
and onActivityResult as follows:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
LISessionManager.getInstance(getActivity()).onActivityResult(getActivity(), requestCode, resultCode, data);
}
Already added hash and package name on LinkedIn developer console. Did I am missing anything? Please help
It is found that the onActivityResult for LinkedIn sdk triggres on the parent activity rather than fragment onActivityResult. So you have to write following code into your parent Activity's onActivityResult for triggering fragment's onActivityResult.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
yourFragment.onActivityResult(requestCode, resultCode, data);
}
Try Log to ensure whether function is called.
I have two activities:
ListViewActivity
AddActivity
and one object class :
todoObj
I have a list view in my ListViewActivity activity and when I click add button this will initiate AddActivity . In AddActivity when user enters Title, choose date, time and the category I want AddActivity to create a todo object and pass this back to the ListViewActivity.
Sorry I could not share the code itself, it always gives this error all the time so I uploaded on github please check it out.
Sorry again.
Many thanks.
All you have to fix onActivityResult method in ListViewActivity:
Because of you setting result code ADD_REQUEST_CODE in AddActivity "setResult(ListViewActivity.ADD_REQUEST_CODE, intent);", you should use "if (resultCode == ADD_REQUEST_CODE) {" in ListViewActivity not RESULT_OK.
You should receive intent from onActivityResult not ListViewActivity's intent. getIntent() gives ListViewActivity's intent. So use data variable:
onActivityResult(int requestCode, int resultCode, Intent data)
Final code:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == ADD_REQUEST_CODE) {
if (resultCode == ADD_REQUEST_CODE) {
Log.i("ListViewActivity", "Returned onActivityResult");
TodoObj todoObj = (TodoObj) data.getParcelableExtra("EXTRA_TODO");
Toast.makeText(ListViewActivity.this, "" + todoObj.getmYear(),
Toast.LENGTH_SHORT).show();
}
}
}
I currently started working on Android and right now i am trying to figure out how to send an Intent From a 2nd Activity to the first!
Now i know that i have to use startActivityForResult and i think i actually used that the right way.
However there is a list on my Main Activity, that i want to fill with a String from my 2nd Activity. But i cant get the List to update without getting a NullPointer Exception when trying to add the received String to the List.
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
I think my issues lay in some Activity Order issues. How do i send the receivedString from OnActivityResult to OnResume once the 2nd activity is finished().
Or The Actualy String coming over from my 2nd activity is always empty for some reason, but i cant figure out why that would be the case...
ANSWER FOUND CHECK POST Beneath
Your problem here is that you're not getting the right intent in the result. Change:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//check for right result
if (requestCode == 10) {
if (resultCode == RESULT_OK) {
//Catching name from 2nd Activity
Intent GoToMain = getIntent();
To:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//check for right result
if (requestCode == 10) {
if (resultCode == RESULT_OK) {
//Catching name from 2nd Activity
Intent GoToMain = data;
Which one is better and why?
This one:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
...
}
or this:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
// do not call super.onActivityResult()
...
}
The first one is better.
It's more consistent with other event functions in the Activity API, it costs you nothing (the code you're calling does nothing at the moment), and it means you don't need to remember to add the call in the future when the behaviour of the base class changes.
Edit
As Su-Au Hwang has pointed out, my prediction about the behaviour of the base class changing in the future has come true! FragmentActivity requires you to call the method on super.
You should call super.onActivityResult if you are using FragmentActivity from the support package (also SherlockFragmentActivity). Otherwise it isn't necessary, however i'd just plug it in there for the sake of it. Check the source of FragmentActivity (no onActivityResult is not empty).
FragmentActivity source
Unless you have multiple subclasses of Activity which depend on it in your application, it doesn't look like calling super.onActivityResult() is needed, since the implementation of onActivityResult() is empty (I checked API level 15).
You can answer this yourself by looking at the source code for Activity.
Basically it's implementation of onActivityResult(...) looks like this...
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
}
...so does nothing.
Although it seems the default implementation is empty, it's possible that in future updates that might not always be the case. I would recommend using it
Calling super is mandatory now. It throws "Overriding method should call super.onActivityResult" error. Adding super won't hurt your code.
Well First you need to understand onActivityResult is a callback when there is an action done with intent like choosing, clicking a photo, etc, and super().onActivityResult() returns an exception when the activity of intent gets crashed or something like that happens.
In short super().onActivityResult() is use to handle the error or exception during execution of Intent.
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
if (!mActivityResultRegistry.dispatchResult(requestCode, resultCode, data)) {
super.onActivityResult(requestCode, resultCode, data);
}
Exception
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
throw new RuntimeException("Stub!");
}