Open Dialog from Notification

后端 未结 1 916
天涯浪人
天涯浪人 2020-12-30 11:51

I have a notification displayed. Now I want this to happen:

When I click on the notification, I would like to open a Dialog, where I print only 1 string.

Now

1条回答
  •  时光说笑
    2020-12-30 12:21

    I do exactly this in one of my apps. In the notification you need to do something like this:

    PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
        new Intent("com.yourcompany.yourapp.MAINACTIVITY").putExtra("fromnotification", true);
    

    Inside your main activity use the onResume() method to check for this extra:

    @Override
        public void onResume()
        {
                super.onResume();
    
                if (getActivity().getIntent().getBooleanExtra("fromnotification", false) == true)
                {
                        getActivity().getIntent().removeExtra("fromnotification");
                        startActivityForResult(
                                        new Intent("com.yourcompany.yourapp.DIALOGACTIVITY"), 123);
                }
    
        }
    

    This code displays an activity with a dialog style, but there is no reason why it can't create a dialog inside the if statement though.

    0 讨论(0)
提交回复
热议问题