如何在Parse Broadcast Receiver的活动中启动我的特定片段?例如,当用户收到通知时,我希望它们立即打开活动A的片段B.这可能吗?我觉得我应该能够使用intent extra ...在onCreate方法中强制从活动A更改片段...
public class Receiver extends ParsePushBroadcastReceiver {
@Override
protected Bitmap getLargeIcon(Context context, Intent intent) {
return BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
}
@Override
protected void onPushOpen(Context context, Intent intent) {
ParseAnalytics.trackAppOpenedInBackground(intent);
String uriString = null;
try {
JSONObject pushData = new JSONObject(intent.getStringExtra("com.parse.Data"));
uriString = pushData.optString("uri");
} catch (JSONException e) {
Log.v(getClass().toString(), "Unexpected JSONException when receiving push data: ", e);
}
Class<? extends Activity> cls = getActivity(context, intent);
Intent activityIntent;
if (uriString != null && !uriString.isEmpty()) {
activityIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(uriString));
} else {
activityIntent = new Intent(context, MainActivity.class);
activityIntent.putExtra("fragment", "fragment2");
activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(activityIntent);
}
activityIntent.putExtras(intent.getExtras());
if (Build.VERSION.SDK_INT >= 16) {
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(cls);
stackBuilder.addNextIntent(activityIntent);
stackBuilder.startActivities();
} else {
activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
activityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(activityIntent);
}
}
}
MainActivity(活动A)
在我的onCreate中,我提出以下内容:
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
if (bundle.getString("fragment") != null) {
/*Log.w(getClass().toString(), bundle.getString("fragment"));*/
viewPager.setCurrentItem(1);
}
}
不幸的是,它并没有自动转移到片段B。