Calling startIntentSenderForResult from Fragment (without using existing Activity) (Android Billing v3)
The default behavior for an Activity
(as seen in the FragmentActivity source for onActivityResult) is to pass onActivityResult
to all attached Fragments' onActivityResult
in addition to the Activity's onActivityResult
. Therefore, just handle the onActivityResult
in your Fragment.
Note: if your Activity does handle onActivityResult
, ensure that it calls super.onActivityResult
or the Fragment's onActivityResult
call will not occur.
There is no onActivityResult() in Fragment. For in-app Billing
These Links are probably helpful
http://code.google.com/p/marketbilling/issues/detail?id=131
Calling startIntentSenderForResult from Fragment (Android Billing v3)
In-App Billing in Fragments
Sorry to disturb Stacked Guys in giving answers,
It is an simple problem I have found out that the fragment is not instantiated at the screen orientation as the screen orientation is locked to portrait
.So whenever I tested that code on tablet the screen of Google Play goes in landscape mode, which in turn causes exception.
Thanks @Nikolay Elenkov for static inner class idea.
PreferenceFragment doesn't get onActivityResult call from in app billing request
The original start request must come from the Fragment
in order for Android to deliver the result back to the same Fragment
. If the Activity
starts the request, the result doesn't inherently get handed to every Fragment
that may be attached to the manager. In addition to this, you have to ensure that if onActivityResult()
is overridden in the top-level Activity
, that you call super.onActivityResult()
as well, or the message won't get delivered to the Fragment
.
The problem with IAB is your given a PendingIntent
instead of a standard Intent
to fire, and there is no method on Fragment
to trigger the initial action even if you can move your code there. To keep things as they are, you may have to do a bit of a swizzle. One thing you could do is make use of a custom interface inside the onAttach()
method of your Fragment
and use it to allow the Fragment
to hand itself up to the Activity
. Something like:
public class SyncPreferencesActivity extends PreferenceActivity
implements SyncPreferencesFragment.OnAttachCallback {
SyncPreferencesFragment mTargetFragment;
@Override
public void onAttachSyncFragment(SyncPreferencesFragment fragment) {
mTargetFragment = fragment;
}
}
...with some additions to the corresponding Fragment
...
public class SyncPreferencesFragment extends PreferenceFragment {
public interface OnAttachCallback {
public void onAttachSyncFragment(SyncPreferencesFragment fragment);
}
@Override
public void onAttach(Activity activity) {
try {
OnAttachCallback callback = (OnAttachCallback) activity;
callback.onAttachSyncFragment(this);
} catch (ClassCastException e) {
throw new ClassCastException("You Forgot to Implement the Callback!");
}
}
}
With something like this, at least you have a reference to the instance so you can forward the result or anything else that might be necessary. If you want to be super clean, you could also implement a matching "detach" that clears the reference in the Activity
.
onIabPurchaseFinished never called.
Try adding this to the Activity that calls mHelper.launchPurchaseFlow(..)
:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d(TAG, "onActivityResult(" + requestCode + "," + resultCode + "," + data);
// 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.");
}
}
Android billing exception
The IabHelper will only allow a single asynchronous query to be executed at a time. You need to implement onActivityResult()
and pass the parameters into the handleActivityResult()
method of the IabHelper.
The in-app billing sample code implements the method like this:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d(TAG, "onActivityResult(" + requestCode + "," + resultCode + "," + data);
// 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.");
}
}
Related Topics
Migration from Gcm to Fcm Needed
Android:What Is Difference Between Setflags and Addflags for Intent
Difference of Setvalue() & Postvalue() in Mutablelivedata
Android Device Chooser -- Device Not Showing Up
How to Change the Track Color of a Switchcompat
Caused By: Java.Lang.Outofmemoryerror: Bitmap Size Exceeds Vm Budget
Rawquery(Query, Selectionargs)
How to Request Root Access in Android
How to Fix "Failed to Sync Vcpu Reg" Error
Interstitial Admob Ads: "Illegalstateexception: Only Fullscreen Activities Can Request Orientation"
Using Google Account to Log in to an Android Application
How to Remove Shared Preference While Application Uninstall in Android
Endless Recyclerview with Progressbar for Pagination
How to Build a Native (Command Line) Executable to Run on Android
Fileprovider Crash - Npe Attempting to Invoke Xmlresourceparser on a Null String
You Cannot Start a Load for a Destroyed Activity in Relativelayout Image Using Glide