1

I have a GameActivity Activity one of whose methods is:

/** Show "Game is finished" dialog. */
public void showFinishedDlg(int bodyMsgRes) {
    GameFinishedDlgFragment.newInstance(bodyMsgRes).show(
            this.getFragmentManager(), "finished_dlg");
}

This is the dialog fragment that method creates:

public class GameFinishedDlgFragment extends DialogFragment {

    static GameFinishedDlgFragment newInstance(int bodyMsgRes) {

        GameFinishedDlgFragment frag = new GameFinishedDlgFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("body_msg", bodyMsgRes);
        frag.setArguments(bundle);
        return frag;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Bundle args = getArguments();
        int bodyMsgRes = args.getInt("body_msg");

        // Use the Builder class for convenient dialog construction
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage(bodyMsgRes)
                .setPositiveButton(R.string.new_game, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // PROBLEM HERE. TODO: Restart GameActivity
                        this.parentActivity.recreate();
                    }
                });

        // Create the AlertDialog object and return it
        return builder.create();
    }
}

I'm trying to make the onClick of the dialog's button restart GameActivity, but I can't figure out how to get the reference to that activity instance.

If I try:

this.parentActivity.recreate();

the compiler tells me:

Cannot resolve symbol 'parentActivity'
DBedrenko
  • 4,871
  • 4
  • 38
  • 73
  • 2
    Create a local variable of Activity type .... pass your activity from onclick which you will get in you new instance method ...init local variable and use – koutuk Jun 19 '15 at 12:38
  • @koutuk I'm very confusde by your comment. Create a local variable where? In `GameActivity.showFinishedDlg()` or inside `OnClick`? You "pass" things to something, not "from" something as you say. – DBedrenko Jun 19 '15 at 12:51
  • 1
    create local variable lets say "Activity localref" in GameFinishedDlgFragment class..... now when ever you call newInstance method/constructor pass another variable along with you message... – koutuk Jun 19 '15 at 12:53
  • @koutuk Ahh I think I get it now, thank you :) – DBedrenko Jun 19 '15 at 12:55
  • i have posted answer below – koutuk Jun 19 '15 at 12:55

3 Answers3

2

Give this a go:

getActivity().recreate();
Simas
  • 43,548
  • 10
  • 88
  • 116
  • Great! Thank you, it compiles now. I didn't know about `getActivity()`. – DBedrenko Jun 19 '15 at 12:44
  • @NewWorld you're welcome :) mark this answer as correct if your problem was resolved. – Simas Jun 19 '15 at 13:07
  • Is there a reason that you do the casting and check that it's a specific `instanceof`? I could write just `getActivity()).recreate()` and it works. – DBedrenko Jun 19 '15 at 13:37
  • 1
    @NewWorld this is just a safety check in case the fragment is used by another activity which does not have that specific method. – Simas Jun 19 '15 at 13:38
  • But all instances of [Activity](http://developer.android.com/reference/android/app/Activity.html#recreate()) have this method. – DBedrenko Jun 19 '15 at 13:42
  • @NewWorld oops you're right. Somehow I thought you're using a custom method. I'll update my answer :) – Simas Jun 19 '15 at 13:43
2

please override the onAttach method of the fragment. The incoming activity param is your parent activity.

lifecycle methods of fragment: http://developer.xamarin.com/guides/android/platform_features/fragments/part_1_-_creating_a_fragment/Images/fragment_lifecycle.png

official reference: http://developer.android.com/reference/android/app/DialogFragment.html#onAttach(android.app.Activity)

or just call getActivity() at everywhere you code, AFTER onAttach has completed! Calling it, before onAttach it gives NULL

(read comments below)

narancs
  • 5,234
  • 4
  • 41
  • 60
  • I like this solution the best, but if in `GameFinishedDlgFragment.OnAttach()` I assign the activity to a field `this.parentActivity`. Then I try to reference it with `this.parentActivity.recreate()` inside `onClick`. The problem is that `this` refers to the `AlertDialog.Builder` not to `GameFinishedDlgFragment`. See the code in my post: it will be clear to you. – DBedrenko Jun 19 '15 at 13:17
  • what about: GameFinishedDlgFragment.this.getActivity() ? – narancs Jun 19 '15 at 13:20
  • Then compiler says "cannot resolve method `recreate()`". I'm not sure what `this` refers to in this case. – DBedrenko Jun 19 '15 at 13:22
  • by the way, what is recreate? I really hope you are not trying to recreate the Activity ! check this out please: http://stackoverflow.com/questions/1397361/how-do-i-restart-an-android-activity – narancs Jun 19 '15 at 13:24
  • But your way with `getActivity()` worked just fine. I was just wondering if there's a Java trick to make `this` refer to the correct thing. – DBedrenko Jun 19 '15 at 13:24
  • Yes I'm following this [answer](http://stackoverflow.com/a/10453257/797744) to restart `GameActivity`. – DBedrenko Jun 19 '15 at 13:26
  • I don`t understand , but the Fragment.this.getActivity().recreate() worked for me! No compiler error! GameFinishedDlgFragment.this is the currently running instance of the GameFinishedDlgFragment class. Did you tried to cast for an Activity ? – narancs Jun 19 '15 at 13:29
  • Yes that worked. What you told me to write before was `GameFinishedDlgFragment.this.getActivity()`. But I can just call `getActivity().recreate()` and it works. – DBedrenko Jun 19 '15 at 13:32
  • I`m happy it`s worked ! If you think I help to solve it, please accept my answer and I adding a @read comments below@ line :D – narancs Jun 19 '15 at 13:34
  • @NewWorld No idea why you'd choose this answer as correct but I guess you know better. Somebody (@Karoly) needs to learn English though :) – Simas Jun 19 '15 at 13:46
  • @NewWorld this "answered 54 mins ago", mine "answered 1 hour ago". Both suggest `getActivity.recreate()`. But nevermind. No problem :) – Simas Jun 19 '15 at 13:53
1
public class GameFinishedDlgFragment extends DialogFragment { 
    private Activity localref;
   static GameFinishedDlgFragment newInstance(int bodyMsgRes,Activity actref)       {
         this.localref=actref;
        GameFinishedDlgFragment frag = new GameFinishedDlgFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("body_msg", bodyMsgRes);
        frag.setArguments(bundle);
        return frag;
    } 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Bundle args = getArguments();
        int bodyMsgRes = args.getInt("body_msg");

        // Use the Builder class for convenient dialog construction 
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage(bodyMsgRes)
                .setPositiveButton(R.string.new_game, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // PROBLEM HERE. TODO: Restart GameActivity 
                       localref.recreate();
                    } 
                }); 

        // Create the AlertDialog object and return it 
        return builder.create();
    } 
} 
koutuk
  • 832
  • 1
  • 8
  • 17