Fragments, how to start a dialogue from a fragment?

I read DialogFragment and create one of them this way.

import android.app.AlertDialog; import android.app.Dialog; import android.content.DialogInterface; import android.os.Bundle; import android.support.v4.app.DialogFragment; public class MyDialogFragment extends DialogFragment { public static MyDialogFragment newInstance(int title) { MyDialogFragment frag = new MyDialogFragment(); Bundle args = new Bundle(); args.putInt("title", title); frag.setArguments(args); return frag; } @Override public Dialog onCreateDialog(Bundle savedInstanceState) { int title = getArguments().getInt("title"); return new AlertDialog.Builder(getActivity()) .setIcon(R.drawable.plus_icon) .setTitle(title) .setPositiveButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { AddExerciseFragment.doPositiveClick(); } } ) .setNegativeButton("NO", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { AddExerciseFragment.doNegativeClick(); } } ) .create(); } } 

and in another Sherlock file I’ll do the following:

 public void doPositiveClick() { } public void doNegativeClick() { } void showDialog() { DialogFragment newFragment = MyDialogFragment.newInstance( R.string.name); newFragment.show(getFragmentManager(), "dialog"); } 

But the doPositiveClick (), doNegativeClick () methods want to be static - this is bad for me.

 public void doPositiveClick() { DialogFlag = 0; Intent photoPickerIntent = new Intent(Intent.ACTION_PICK); photoPickerIntent.setType("image/*"); startActivityForResult(photoPickerIntent, GALLERY_REQUEST); // dialog.cancel(); } @Override public void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { super.onActivityResult(requestCode, resultCode, imageReturnedIntent); String path = null; if (DialogFlag == 0) { switch (requestCode) { case GALLERY_REQUEST: if (resultCode == RESULT_OK) { Uri selectedImage = imageReturnedIntent.getData(); path = getRealPathFromURI(selectedImage); Log.d("myLogs", path); if (btnID == 1) { pathOne = path; Bitmap bmImg = BitmapFactory.decodeFile(pathOne); ivOne.setImageBitmap(bmImg); one = bmImg; } else { pathTwo = path; Bitmap bmImg = BitmapFactory.decodeFile(pathTwo); ivTwo.setImageBitmap(bmImg); two = bmImg; } } } } if (DialogFlag == 1) { Uri uri; if (requestCode == CAMERA_RESULT) { Cursor cursor = getActivity().getContentResolver().query( Media.EXTERNAL_CONTENT_URI, new String[] { Media.DATA, Media.DATE_ADDED, MediaStore.Images.ImageColumns.ORIENTATION }, Media.DATE_ADDED, null, "date_added ASC"); if (cursor != null && cursor.moveToFirst()) { do { uri = Uri.parse(cursor.getString(cursor .getColumnIndex(Media.DATA))); path = uri.toString(); } while (cursor.moveToNext()); cursor.close(); } Log.d("myLogs", path); if (btnID == 1) { pathOne = path; Bitmap bmImg = BitmapFactory.decodeFile(pathOne); ivOne.setImageBitmap(bmImg); one = bmImg; } else { pathTwo = path; Bitmap bmImg = BitmapFactory.decodeFile(pathTwo); ivTwo.setImageBitmap(bmImg); two = bmImg; } } } } 
+2
android static android-fragments dialog
Jul 31 '13 at 9:29
source share
2 answers

In your fragment class

Announce below

 public static final int DIALOG_FRAGMENT = 1; public static final int RESULT_OK = 101; 

Then

  DialogFragment newFragment = MyDialogFragment.newInstance( R.string.name); newFragment.setTargetFragment(SherLockFragmentName.this, DIALOG_FRAGMENT); newFragment.show(getFragmentManager(), "dialog"); 

Then in the dialog fragment

  .setPositiveButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { Intent i =getActivity().getIntent(); i.putExtra("key", true); getTargetFragment().onActivityResult(getTargetRequestCode(), 101, i); } } ) .setNegativeButton("NO", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { //AddExerciseFragment.doNegativeClick(); Intent i =getActivity().getIntent(); i.putExtra("key", false); getTargetFragment().onActivityResult(getTargetRequestCode(), 101, i); } } ) 

Then override onActivityResul t in the fragment class

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { switch(requestCode) { case DIALOG_FRAGMENT: if (resultCode == RESULT_OK) { boolean check = data.getBooleanExtra("key", true); if(check) { dopositiveClick(); } else { donegativeClick(); } } break; } } 
+3
Jul 31. '13 at 10:33
source share

If you do not want another option to appear in static access:

 @Override public Dialog onCreateDialog(Bundle savedInstanceState) { int title = getArguments().getInt("title"); final AddExerciseFragment aef = new AddExerciseFragment(SomeParameters...); return new AlertDialog.Builder(getActivity()) .setIcon(R.drawable.plus_icon) .setTitle(title) .setPositiveButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton){ aef.doPositiveClick(); } //and so on 
0
Jul 31 '13 at 9:39 on
source share



All Articles