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; } } } }
android static android-fragments dialog
Kostya Khuta Jul 31 '13 at 9:29 am 2013-07-31 09:29
source share