gpt4 book ai didi

java - onClick 通知 fragment Android

转载 作者:行者123 更新时间:2023-12-01 17:54:49 25 4
gpt4 key购买 nike

我正在研究通知部分。我能够将用户导航到通知消息的单击 Activity 。我的实际任务是将用户导航到 fragment 。

这是工作正常的通知代码。

NotificationCompat.Builder builder =
new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Lugmah Order Status")
.setContentText("The Order Status of Order Id: "+selectedOrderId+ " is: "+status_desc)
.setDefaults(NotificationCompat.DEFAULT_SOUND)
.setAutoCancel(true);
int NOTIFICATION_ID = 12345;

Intent targetIntent = new Intent(getApplicationContext(), MainActivity.class);

targetIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

targetIntent.putExtra("isTrackOrder", false);
targetIntent.putExtra("orderNotification", "orderNotification");
targetIntent.putExtra("isLoggedIn", true);
targetIntent.putExtra("status_desc", status_desc);
targetIntent.putExtra("selectedOrderId", selectedOrderId);

PendingIntent contentIntent = PendingIntent
.getActivity(getApplicationContext(), 0, targetIntent, PendingIntent.FLAG_ONE_SHOT);

builder.setContentIntent(contentIntent);


NotificationManager nManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
nManager.notify(NOTIFICATION_ID, builder.build());

我在 Intent 中添加了MainActivity.java,因为这是我加载所有 fragment 的地方。在这次 Activity 中,我做了这样的事情。

这是 onCreate() 中的代码:

String orderNotification = getIntent().getStringExtra("orderNotification");

android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();

if (orderNotification != null)
{
if (orderNotification.equals("orderNotification"))
{
TrackOrderFragment trackOrderFragment = new TrackOrderFragment();
fragmentTransaction.replace(android.R.id.content, trackOrderFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}

如果有帮助,我们将不胜感激。

最佳答案

您的通知代码工作正常,您需要稍微调整一下 onCreate 代码。

if(getIntent().getExtras() != null) {
String orderNotification = getIntent().getStringExtra("orderNotification");

if (orderNotification.equals("orderNotification"))
{
TrackOrderFragment trackOrderFragment = new TrackOrderFragment();
fragmentTransaction.replace(android.R.id.content, trackOrderFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}

您需要在每次 Activity 运行 onCreate 时检查是否收到任何额外内容,因为可能会发生空指针异常。

关于java - onClick 通知 fragment Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46052854/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com