onNewIntenet不在SherlockFragment中,因此在Fragment上点击通知不会改变视图

问题描述 投票:0回答:1

我现在处于困境,我无法决定去哪里。我有一个应用程序,我在后台服务中收到XMPP数据包,并从那里我发出通知。这些通知是这样的

     int icon = R.drawable.ic_launcher;
     long when = System.currentTimeMillis();
     NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
     Notification notification = new Notification();
     notification.when = when;
     notification.icon = icon;
     Intent notificationIntent = new Intent(context,MainActivity.class);
     notificationIntent.putExtra("title", from);
     notificationIntent.putExtra("message", message);
     notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
     PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
     notification.setLatestEventInfo(context, message  + " from " + from, "", intent);
     notification.flags |= Notification.FLAG_AUTO_CANCEL; 
     notification.defaults |= Notification.DEFAULT_SOUND; 
     notification.defaults |= Notification.DEFAULT_VIBRATE;        
     notificationManager.notify(0, notification);  

因为有三个片段(MainActivity中的SherlockFragments)所以当通知到来且app在后台时。当我点击通知时,它会显示Intent中的值,但是如果应用程序位于前台并且我单击通知,则它不会显示该值。似乎没有任何事情发生,因为onCreateView不会被多次调用。如何处理这些数据,以便我可以在应用程序上看到通知中的数据?

我在onCreateView上从Internet获取数据,如下所示

if(getActivity().getIntent().getStringExtra("title") != null)
    {   showMessage(getActivity().getIntent().getStringExtra("title"),getActivity().getIntent().getStringExtra("message"));
    }

showMessage()

public void showMessage(String title, String message)
{
    messages.add(title + ":");
    messages.add(message);
    // Add the incoming message to the list view
    mHandler.post(new Runnable() {
        public void run() {
            //setListAdapter();//update the listview
        }
    });
}
android android-intent android-fragments notifications actionbarsherlock
1个回答
1
投票
@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);

    setIntent(intent);

    String title = getIntent().getIntExtra("title");
    String message = getIntent().getIntExtra("message");

   ///here replace fragment with data
   TestFragment testfrag=new TestFragment();
   Bundle bundle=new Bundle();
   bundle.putString("Title",title);
   bundle.putString("Message",message);
   testfrag.setArguments(bundle);
   getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.home_container,
                            testfrag)

}
© www.soinside.com 2019 - 2024. All rights reserved.