Android 单击从片段到另一个片段的通知

Android 单击从片段到另一个片段的通知,android,android-fragments,Android,Android Fragments,我是android的新开发者。我在片段和通知方面有困难。这是我的一些代码 test.java public class test extends Fragment { \\some of codes here up. @Override public View onCreateView(LayoutInflater, ViewGroup container, Bundle savedIntancesState) { View view = inflater.inflate(R.layout.

我是android的新开发者。我在片段和通知方面有困难。这是我的一些代码

test.java

public class test extends Fragment {
 \\some of codes here up.

@Override
public View onCreateView(LayoutInflater, ViewGroup container, Bundle savedIntancesState) {
View view = inflater.inflate(R.layout.test_fragment, container, false);
 Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    FragmentManager fragmentManager = getActivity().getSupportFragmentManager();

    PendingIntent resultPendingIntent = PendingIntent.getActivity(getActivity(), fragmentManager.beginTransaction().replace(R.id.flContent, NextFragment.newInstance("message", R.layout.content_row)).commit(), getActivity().getIntent(), PendingIntent.FLAG_UPDATE_CURRENT);

    Notification notification = new NotificationCompat.Builder(getActivity())
            .setContentTitle(title)
            .setContentText(message)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentIntent(resultPendingIntent)
            .setSound(soundUri)
            .addAction(R.mipmap.ic_launcher, "Call",resultPendingIntent).build();
    NotificationManager notificationManager = (NotificationManager) getActivity().getSystemService(NOTIFICATION_SERVICE);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(0,notification);

    return view;
 }

我希望首先显示测试片段并有一个通知,但该代码的结果是NextFragment类始终显示。请告诉我哪里出错了。谢谢你的帮助。非常感谢。

创建一个单独的类来设置扩展BroadcastReceiver的alarm,并从片段中调用它。请参阅以下链接:-

编辑:-

充气测试碎片布局:-

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.test_fragment, container, false);
        //your code for notification
        return view;
    }

创建一个单独的类来设置扩展BroadcastReceiver的alarm,并从片段中调用它。请参阅以下链接:-

编辑:-

充气测试碎片布局:-

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.test_fragment, container, false);
        //your code for notification
        return view;
    }

谢谢你的回答,但我为什么需要闹钟呢?我只希望测试片段类将是第一个显示的,实际上测试片段类在抽屉中。我认为问题在于这行:-PendingEvent ResultPendingEvent=PendingEvent.getActivity(getActivity(),fragmentManager.beginTransaction().replace(R.id.flContent,NextFragment.newInstance(“message”,R.layout.content\u row)).commit()、getActivity()、getIntent()、PendingIntent.FLAG_UPDATE_CURRENT);您正在用NextFragment替换fragmentManager,用TestFragmentyeah替换它。您是正确的,但是,当我单击通知时,它不会将我指向NextFragment。这也是问题所在,我希望单击时的通知将转到下一个片段谢谢您的回答,但为什么我需要警报?我只希望测试片段类将是第一个显示的,实际上测试片段类在抽屉中。我认为问题在于这行:-PendingEvent ResultPendingEvent=PendingEvent.getActivity(getActivity(),fragmentManager.beginTransaction().replace(R.id.flContent,NextFragment.newInstance(“message”,R.layout.content\u row)).commit()、getActivity()、getIntent()、PendingIntent.FLAG_UPDATE_CURRENT);您正在用NextFragment替换fragmentManager,用TestFragmentyeah替换它。您是正确的,但是,当我单击通知时,它不会将我指向NextFragment。这也是问题所在,我希望单击时的通知将转到NextFragment