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!

9
  • Some pieces of code will be easier to understand your question. Commented Mar 17, 2016 at 2: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 Commented Mar 17, 2016 at 2:21
  • 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 Commented Mar 17, 2016 at 2:23
  • 1
    Please also show where the Dialog is created Commented Mar 17, 2016 at 2:28
  • 1
    @The_Grits What have you tried to call in positiveButton callback? Commented Mar 17, 2016 at 2:28

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, this makes a lot of sense. I'll vote your answer up and test it tomorrow. Thanks again!
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!
Great. If you are doing it in a fragment that's the way to go! I'm glad I was useful.
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();
}

1 Comment

thx cricket_007. didn't found the time to simplify my expression. Sorry :D
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!

Comments