2

i read DialogFragment, and make one in one like this.

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 SherlockFragment i make next:

public void doPositiveClick() {


}

public  void doNegativeClick() {


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

But methods doPositiveClick(),doNegativeClick() wants to be static - it 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;
            }

        }
    }
}
Kostya Khuta
  • 1,846
  • 6
  • 26
  • 48
  • i can't call getActivity() – Kostya Khuta Jul 31 '13 at 09:41
  • why is that? http://developer.android.com/guide/topics/ui/controls/pickers.html. check the timepicker which is a static class. Also if you have a inner class as static you can use a weakreference to the activity context – Raghunandan Jul 31 '13 at 09:45
  • i can't call getActivity in Fragments static method, they uses getActivity in non static method – Kostya Khuta Jul 31 '13 at 09:47
  • check my post. i used a boolean value. i get the value in fragment on click on buttons in dialog fragment. based on the boolean value call positive click method or negative click method. – Raghunandan Jul 31 '13 at 10:34

2 Answers2

4

In your Fragment class

Declare the 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 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 onActivityResult in 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;
        }
    }
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • "Then override onActivityResult in Fragment class" i can't ovveride this method twice. I have one inActivityResult,look in my question – Kostya Khuta Jul 31 '13 at 11:02
  • don't override twice override once use a if else like you did before? or switch case. – Raghunandan Jul 31 '13 at 11:09
  • if you feel this is hard use a interface and implement a interface in your fragment. based on the boolean values call the appropriate methods – Raghunandan Jul 31 '13 at 11:15
  • it is hard for me now, because i am programing about 18 hours, and now it is so many variables) – Kostya Khuta Jul 31 '13 at 11:31
  • @KostyaKhuta if its hard you should rethink your design to make it simple. I would definitely rethink about changing the design. – Raghunandan Jul 31 '13 at 11:34
  • and the last, i press ok(positive), key = true -> getTargetFragment().onActivityResult() -> this is checked and that it call doPositive..() -> StartActivityForResult-> and where i must save the result?? – Kostya Khuta Jul 31 '13 at 11:36
  • i am totally lost i can't comprehend what's exactly you want. pls ask a new question. i din't understand your comment. What is the result? and what do you want to store. – Raghunandan Jul 31 '13 at 11:38
  • ok, wait one minute, i will post new question, and i will check your answer) – Kostya Khuta Jul 31 '13 at 11:42
0

If you don't want want a static access here's another option:

@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
Horschtele
  • 109
  • 3
  • but when i startActivityForResult from doPositiveClick() - i get error AddExerciseFragment not attached to Activity – Kostya Khuta Jul 31 '13 at 09:58
  • @KostyaKhuta http://developer.android.com/training/basics/fragments/communicating.html – Raghunandan Jul 31 '13 at 10:01
  • @KostyaKhuta Quoting from the docs **All Fragment-to-Fragment communication is done through the associated Activity. Two Fragments should never communicate directly.** – Raghunandan Jul 31 '13 at 10:06
  • Can you help me? I start Dialog from FragmentA, when i press positive button - i must startActivityForResult - result will be saved in this FragmentA. I edit my questinn with two methods. – Kostya Khuta Jul 31 '13 at 10:14
  • 1
    @KostyaKhuta http://stackoverflow.com/questions/10905312/receive-result-from-dialogfragment. check this might help. – Raghunandan Jul 31 '13 at 10:14
  • @KostyaKhuta posting it as an answer check my post. – Raghunandan Jul 31 '13 at 10:29