0

I am making an gaming app that brings up an AlertDialog when the game is over. Inside the dialog, there is a positiveButton to play again.

What I would like the button to call the functions I have already made for the onCreate() method, but I do not want the functions to be static.

Here is what my onCreate method looks like:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setup();
}

I would like to call the setup() function from the dialog.

How would one go about this?

Thanks in advance!

  • Some pieces of code will be easier to understand your question. – toantran Mar 17 '16 at 02:21
  • You shouldn't be calling onCreate because the Activity is already created. Please edit your question to include what you are trying to do – OneCricketeer Mar 17 '16 at 02:21
  • @cricket_007 I am not calling `onCreate`, I'm just calling the same functions I call within the `onCreate` method. –  Mar 17 '16 at 02:22
  • 1
    You should either be able to do `MyActivity.this.foo()` or `foo()` directly. I don't understand why you wouldn't be able to unless your Dialog is not in the Activity class – OneCricketeer Mar 17 '16 at 02:23
  • @autobot_101 I have edited the question to include some code. –  Mar 17 '16 at 02:26
  • 1
    Please also show where the Dialog is created – OneCricketeer Mar 17 '16 at 02:28
  • 1
    @The_Grits What have you tried to call in `positiveButton` callback? – toantran Mar 17 '16 at 02:28
  • Why don't you post the code of the dialog so we can see what you are doing. Note that this in an anonymous object refers to the object encapsulating the call. – JoxTraex Mar 17 '16 at 02:33
  • I can't access the code as of right now, but I can tell you all that I have an `onClickListener` on `this` (referring to the `positiveButton`) that I was trying to call `setup()` in the `onClick` function. –  Mar 17 '16 at 02:36

3 Answers3

1

It doesn't have to be static. If your callback for positive button is an anonymous or a non-static inner class than it can access non static methods of the outer class. You can just call setup() from the callback.

Of course, this won't be the case if you are not using anonymous or non-static inner callback class in which case you have to make the Activity method static to be able to call it.

This answer shows how to do it.

Community
  • 1
  • 1
arsent
  • 6,975
  • 3
  • 32
  • 31
  • 1
    Thank you, this makes a lot of sense. I'll vote your answer up and test it tomorrow. Thanks again! –  Mar 17 '16 at 02:37
  • Your answer didn't solve my problem, but it did get me a lot closer. If you want to see what worked, I'm posting a solution. Thanks for your help! –  Mar 17 '16 at 11:14
  • Great. If you are doing it in a fragment that's the way to go! I'm glad I was useful. – arsent Mar 17 '16 at 15:02
0

If you use an DialogFragment as I do in my latest project you can create an interface. For example:

/// Inside my DialogFragment
public interface ChooserListener {
    public void setupDialogListener();
}

// Inside my DialogFragment
@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);

    try {
        mListener = (ChooserListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()+" must implement the ChooserListener interface.");
    }
}

And then you can just implement the interface into your application with ... implements YourDialogFragment.YourListener. Now you must implement the predefined functions in your listener into your main activity and you can simply trigger them from inside of the Dialog. For further reference see Dialogs - Passing Events

In your case you should write a function like this:

public void setupDialogListener() {
     setup();
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Georg Friedrich
  • 183
  • 2
  • 12
0

With the basic idea off of @arsent's answer, this is what worked for me:

Main mainActivity = (Main)getActivity();
mainActivity.setup();

Thanks everybody for the help!