-1

I have a Service which is executed once in every 5 minute.It shows an AlertDialog when executed.I want to start a Fragment on AlertDialog button click.I tried to start the Fragment like

android.app.FragmentManager fm = ((Activity) getApplicationContext()).getFragmentManager();

but gives exception java.lang.ClassCastException: android.app.Application cannot be cast to android.app.Activity.

This is my code for AlertDialog:

Handler mHandler = new Handler(getMainLooper());
                        mHandler.post(new Runnable() {
                            @Override
                            public void run() {
                                final AlertDialog alertDialog = new AlertDialog.Builder(getApplicationContext(),R.style.Theme_AppCompat_Light_Dialog_Alert)
                                .create();

                             // Setting Dialog Title
                                alertDialog.setTitle(eventNameEnglish);

                                // Setting Dialog Message

                                alertDialog.setMessage(eventAddressEnglish+"\n"+duration);


                        // Setting  View Details Button
                        alertDialog.setButton(AlertDialog.BUTTON_POSITIVE,"View Details", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                android.app.FragmentManager fm = ((Activity) getApplicationContext()).getFragmentManager();

                                dialog_fragment = new DetailFragment("Events",
                                        eventNameEnglish, eventAddressEnglish,
                                        eventContactNumberEnglish, eventEmailEnglish, eventFaxEnglish,
                                        duration,eventOrganizerNameEnglish,
                                        eventDescriptionEnglish,eventFullDescriptionEnglish,
                                        eventLocationEnglish,eventImageEnglish,
                                        eventWebsiteEnglish
                                        );

                                dialog_fragment.show(fm, "detailScreen");
                            }
                        });

                        // Setting Cancel Button
                                alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE,"Cancel", new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int which) {
                                        alertDialog.dismiss();
                                    }
                                });

                        // Showing Alert Message
                        alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
                        alertDialog.show();
                            }
                        });

How to do this?Please help.

Jas
  • 3,207
  • 2
  • 15
  • 45

3 Answers3

3

The exception tells the problem. getApplicationContext return value cannot cast to Activity. You have to taget an existing activity.

I think you need some communication between the service and the activity.

You can use Eventbus or broadcastreceiver or something like that.

http://developer.android.com/reference/android/content/BroadcastReceiver.html

balint.steinbach
  • 278
  • 3
  • 16
  • So i can't start Fragment directly from Service? – Jas Oct 06 '15 at 08:46
  • You can start activity from service: http://stackoverflow.com/questions/3606596/android-start-activity-from-service – balint.steinbach Oct 06 '15 at 08:47
  • The link you provided is for starting Activity from Service not Fragment – Jas Oct 06 '15 at 08:49
  • You can "put" the Fragment on the Activity. If the Activity instance already exists you can send a broadcast event to do something with the fragment on the Activity. If the activity instance not exist, you have to start it. – balint.steinbach Oct 06 '15 at 08:50
  • I used BroadCastReceiver and it is working.Thanks for the answer :) – Jas Oct 06 '15 at 09:33
  • Suppose i have two AlertDialogs waiting to be displayed.When i clicked on View details of first dialog,the detail fragment pops up but the second alert dialog comes on top of that dialog.Do you have any idea how to handle this? – Jas Oct 06 '15 at 09:57
1

You cannot show a Fragment alone, A Fragment can only display UI embedded within an Activity. So, you question is a bit to broad to be answered properly.

However, here are a couple of pointers:

  1. write a, activity_layout with a frame_layout as placeholder for your fragment
  2. write an Activity with activity_layout as content
  3. register the activity in your AndroidManifest.xml
  4. call startActivity on your service with a proper Intent
  5. in the onCreate method of your Activity, you can get a FragmentManager and then show your fragment.
Sascha Kolberg
  • 7,092
  • 1
  • 31
  • 37
0

you can start activity , then only you can show the fragment from that. This is because without activity , getFragmentManager() will return null

Sangeet Suresh
  • 2,527
  • 1
  • 18
  • 19