我有一个MainActivity,它有一个布尔变量mMonthlySubscribed。此活动有一个onPurchasesUpdated回调方法,该方法将true或false设置为mMonthlySubscribed变量。还有loadFragment方法在Fragments之间导航。
我的应用中有2个片段,分别为FragmentSubscribed和FragmentUnsubscribed。我如何通知mMonthlySubscribed值已更改为Fragment。
目前我正在检查mMonthlySubscribed值,并使用以下方法导航到相应的片段。当在MainActivity中触发onPurchasesUpdated回调并更改boolean mMonthlySubscribed时,我想从我的Fragments中调用以下方法之一。
((MainActivity) getActivity()).loadFragment(new FragmentUnsubscribed());
和
((MainActivity) getActivity()).loadFragment(new FragmentSubscribed());
简单来说,我的问题是当触发活动onPurchasesUpdated回调时,如何通知当前片段,以便我可以相应地调用上述方法。
MainActivity&#39>回调
@Override
public void onPurchasesUpdated(List<Purchase> purchaseList) {
for (Purchase purchase : purchaseList) {
if (purchase.getSku().equals(SKU_MONTHLY)) {
Toast.makeText(getApplicationContext(), "Subscription is valid! - server", Toast.LENGTH_SHORT).show();
mMonthlySubscribed = true;
} else {
Toast.makeText(getApplicationContext(), "Subscription is invalid! - server", Toast.LENGTH_SHORT).show();
mMonthlySubscribed = false;
}
}
// save state
saveSubscriptionStateSharedPreference();
// todo : notify the current state to the fragment
}
MainActivity的导航方法
public void loadFragment(Fragment fragment) {
// load passed fragment within the frame_container of activity_main
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_container, fragment);
transaction.commit();
}
答案 0 :(得分:0)
据我所知,你可以试试这个:
public void onPurchasesUpdated(List<Purchase> purchaseList) {
for (Purchase purchase : purchaseList) {
if (purchase.getSku().equals(SKU_MONTHLY)) {
Toast.makeText(getApplicationContext(), "Subscription is valid! - server", Toast.LENGTH_SHORT).show();
mMonthlySubscribed = true;
ParsePush.subscribeInBackground("SignalChannel");
((MainActivity) getActivity()).loadFragment(new FragmentSubscribed());
} else {
Toast.makeText(getApplicationContext(), "Subscription is invalid! - server", Toast.LENGTH_SHORT).show();
mMonthlySubscribed = false;
ParsePush.unsubscribeInBackground("SignalChannel");
((MainActivity) getActivity()).loadFragment(new FragmentUnsubscribed());
}
}
// save state
saveSubscriptionStateSharedPreference();
// todo : notify the current state to the fragment
}
为什么不从loadFragment
onPurchasesUpdated
方法
答案 1 :(得分:0)
我设法使用LocalBroadcastManager完成了这项工作。我将发布我的答案,目的是帮助那些正在寻找这种用例解决方案的其他人。
在我的MainActivity中,我实现了sendMessage()方法来发送本地广播。
public static Uri GetUri(this HttpRequest request)
{
var uriBuilder = new UriBuilder
{
Scheme = request.Scheme,
Host = request.Host.Host,
Port = request.Host.Port.GetValueOrDefault(80),
Path = request.Path.ToString(),
Query = request.QueryString.ToString()
};
return uriBuilder.Uri;
}
然后在onPurchasesUpdated()的回调中,我正在发送上述广播消息。如下所示,
private void sendMessage() {
Log.d("sender", "Broadcasting message subscription state update");
Intent intent = new Intent("subscription_state");
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
在每个片段中,我在下面的监听器中实现了以下给出的上述广播消息。
@Override
public void onPurchasesUpdated(List<Purchase> purchaseList) {
for (Purchase purchase : purchaseList) {
if (purchase.getSku().equals(SKU_MONTHLY)) {
Log.d(TAG, "Subscription is valid! - server");
mMonthlySubscribed = true;
}
}
//// save state and notify the current state to the fragment
saveSubscriptionStateSharedPreference();
sendMessage();
}
当广播接收器接收到广播消息时,将触发每个片段中实现的onReceive()。
因此,当触发onPurchasesUpdated()回调方法时,MainActivity使用sendMessage()发送本地广播消息。每个片段都侦听此广播消息,一旦收到消息,它就会触发loadFragment()并导航到相应的片段。