android - How to get parent activity reference in fragment constructor? -
i have gameactivity
activity
1 of methods is:
/** show "game finished" dialog. */ public void showfinisheddlg(int bodymsgres) { gamefinisheddlgfragment.newinstance(bodymsgres).show( this.getfragmentmanager(), "finished_dlg"); }
this dialog fragment 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 builder class 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 alertdialog object , return return builder.create(); } }
i'm trying make onclick
of dialog's button restart gameactivity
, can't figure out how reference activity instance.
if try:
this.parentactivity.recreate();
the compiler tells me:
cannot resolve symbol 'parentactivity'
please override onattach method of fragment. incoming activity param 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 call getactivity() @ everywhere code, after onattach has completed! calling it, before onattach gives null
(read comments below)
Comments
Post a Comment