preferenceFragment没有得到onActivityResult从应用内结算请求调用 [英] PreferenceFragment doesn't get onActivityResult call from in app billing request

查看:146
本文介绍了preferenceFragment没有得到onActivityResult从应用内结算请求调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个preference屏幕,presents有一个复选框禁用的广告用户。当用户点击这个是第一次,他们是psented与在应用程序内结算的购买选项来禁用广告$ P $。

I have a preference screen that presents the user with a checkbox to disable ads. When the user clicks this for the first time, they are presented with an In App Billing purchase option to disable the ads.

我在这里面临的问题是,我看不出有什么办法让onActivityResult回调到片段。

The issue I'm facing here is that I can't see any way to get the onActivityResult callback into the fragment.

所以我有一个preferenceActivity加载preferenceFragment(其中我似乎无法得到一个参考)。在应用程序内结算需要调用 startIntentSenderForResult ,该片段没有,只有活动。

So I have a PreferenceActivity loading a PreferenceFragment (of which I can't seem to get a reference). In App Billing requires a call to startIntentSenderForResult, which Fragments don't have, only Activities.

当我启动与 startIntentSenderForResult 的购买流程,活动的 onActivityResult 被调用,但我需要这样的片段。

When I launch the purchase flow with startIntentSenderForResult, the Activity's onActivityResult gets called, but I need this in the fragment.

由于我装了preferenceFragment到preferenceActivity用下面的,我不认为我可以得到一个参考片段通过电话了。

Because I loaded the PreferenceFragment into the PreferenceActivity with the following, I don't think I can get a reference to the Fragment to pass the call down.

@Override
public void onBuildHeaders(List<Header> target) {
    loadHeadersFromResource(R.layout.preferences_headers, target);
}

@Override
public Intent getIntent() {
    final Intent modIntent = new Intent(super.getIntent());
    modIntent.putExtra(EXTRA_SHOW_FRAGMENT, SyncPreferencesFragment.class.getName());
    modIntent.putExtra(EXTRA_NO_HEADERS, true);
    return modIntent;
}

我在想什么吗?我不'要拆了我所有的购买逻辑,让我怎么得到我的片段,以获得 onActivityForResult 打电话?

推荐答案

原开始请求必须来自片段,以便为Android提供的结果返回给同样片段。如果活动启动的要求,其结果本身并没有拿到手的每一个片段可连接到经理。除了这个,你必须确保如果 onActivityResult()的顶层被覆盖活动,即你叫 super.onActivityResult()为好,或者消息不会被传递到的片段

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.

与IAB的问题是你给一个 PendingIntent ,而不是标准意图来火了,也没有在片段方法来触发初始动作,即使你可以将你的code那里。为了保持事物的本来面目,你可能需要做的有点调酒的。有一件事你可以做的是利用你的片段的 onAttach()方法中的自定义界面的,并用它让片段交出自己到活动。是这样的:

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;
    }
}

......与一些附加到相应的片段 ...

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.

这篇关于preferenceFragment没有得到onActivityResult从应用内结算请求调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆