0

I have a custom class that extends a View, where I draw some geometric objects on the canvas. I also have a dialog class where I display a simple dialog. (FinePartita.java)

My MainActivity.java (from where I call my View)

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    PaschiPongView ppv = new PaschiPongView(getApplicationContext());
    setContentView(ppv);
}

}

PaschiPongView.java

public class PaschiPongView extends View {
// a lot of code here
}

FinePartita.java (the example is from Google)

public class FinePartita extends DialogFragment {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the Builder class for convenient dialog construction
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setMessage(R.string.doUreallyWantToExit)
            .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // FIRE ZE MISSILES!
                }
            })
            .setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // User cancelled the dialog
                }
            });
    // Create the AlertDialog object and return it
    return builder.create();
}
}

The problem is, I need to display the dialog in my PaschiPongView class, but I can't do it because it needs a FragmentManager and since my class extends a View, it doesn't have it.

I can't call it like this:

FinePartita test = new FinePartita();
test.show(getFragmentManager(), "dialog"); // doesn't work

Any suggestions would be appreciated.

user2729661
  • 9
  • 1
  • 1
  • 6
  • 1
    What do you mean by doesn't work. Does it compile or is there any runtime exceptions on your logcat? – interlude Apr 20 '14 at 10:28
  • @interlude there's no such method as getFragmentManager() in my class that extends View (PaschiPongView). Only my MainActivity class has that. – user2729661 Apr 20 '14 at 10:33

2 Answers2

0

First, I'll answer your answer directly. This is a bit dirty, but does seem safe:

The Context object of a View is always the Activity holding it. So casting Activity to the result of the getContext() should do the trick:

((Activity) getContext()).getFragmentManager();

Second, let's talk about why you SHOULDN'T do this: As a rule, I never let my views handle business logic roles. Why? Because I don't wanna tie general use cases to a specific view. Doing so, I.E. letting your views affect the flow of the application, means keeping track of application state is hard, and changing flows is harder. And also code duplication. And ties you to UI design decisions that might not be applicable in the future.

I always implement a master controller that the views report their state to. Let's call it FlowControl. This is the object in charge of deciding what dialogs to show, how to handle results and so forth. Also, this is a good place to store a reference to FragmentManager. See where I'm going with this?

Vaiden
  • 15,728
  • 7
  • 61
  • 91
  • Yes, thank you. I'd be grateful if you'll provide some simple code snippets regarding the "FlowControl" and how it can help me. – user2729661 Apr 20 '14 at 10:41
  • Regarding FlowControl - it's hard providing a code snippet to something which is so specific. In general - think of a singleton which holds reference to a FragmentManager and also has an onActivityResult() method. So all decisions regarding "where to go next" are made by it. – Vaiden Apr 20 '14 at 10:45
  • unfortunately it says java.lang.ClassCastException: android.app.Application cannot be cast to android.app.Activity :( – user2729661 Apr 20 '14 at 10:58
  • How can a view have a Context which can not handle GUI? This means you're creating your views in a wrong way. http://stackoverflow.com/questions/8276634/android-get-hosting-activity-from-a-view – Vaiden Apr 20 '14 at 11:07
  • Also: http://stackoverflow.com/questions/987072/using-application-context-everywhere – Vaiden Apr 20 '14 at 11:10
  • Checked in one of my custom views just to be sure - my answer seems to work fine. Something is wrong with the way you instantiate PaschiPongView. – Vaiden Apr 20 '14 at 11:17
0

DialogFragment is a Fragment which shows dialogs. If you want to show the fragment you need a FragmentManager which can be accessed using Activity's getFragmentManager() method.

DialogFragment's show method will show the Fragment not the Dialog.

interlude
  • 843
  • 8
  • 29