3

There is a DialogFragment (SettingsBackImageDialog) opens by click on button in Fragment (TrainerSettings) in MainActivity. In SettingsBackImageDialog are some buttons, one of them is for take picture and set it for Imageview (trainersettingsmainicon) in Fragment.

I open SettingsBackImageDialog in TrainerSettings by:

    public void onViewCreated (View view, Bundle savedInstanceState) {    
            view.findViewById(R.id.trainersettingsbackgroundbtn).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showEditbackImageDialog();
                }
            });
        }

private void showEditbackImageDialog() {
        android.support.v4.app.FragmentManager fm = getActivity().getSupportFragmentManager();
        SettingsBackImageDialog settingsBackImageDialog = SettingsBackImageDialog.newInstance("Wybierz pierwszy kolor");
        settingsBackImageDialog.show(fm, "SettingsBackImageDialog");
    }

My code from SettingsBackImageDialog is:

    public class SettingsBackImageDialog extends DialogFragment {

        int REQUESTCODE=1;

        public SettingsBackImageDialog() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            return inflater.inflate(R.layout.settingsbackgroundimgdialog, container);
        }

        @Override
        public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);

            view.findViewById(R.id.takephotobtn).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(i, REQUESTCODE);
                }
            });

            String title = getArguments().getString("txt", "txt");
            getDialog().setTitle(title);
            getDialog().getWindow().setSoftInputMode(
            WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
        }


        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            Toast.makeText(getActivity(), "before", Toast.LENGTH_LONG).show();
            if(requestCode==REQUESTCODE & resultCode== Activity.RESULT_OK){
                Bundle bundle = new Bundle();
                bundle = data.getExtras();
                Bitmap bitmap = (Bitmap)bundle.get("data");
                ImageView img = (ImageView) getActivity().findViewById(R.id.trainersettingsmainicon);
                img.setImageBitmap(bitmap);
                Toast.makeText(getActivity(), "in", Toast.LENGTH_LONG).show();
            }
        }

    }

In MainActivity:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.MainContainer);
    fragment.onActivityResult(requestCode, resultCode, data);
    Toast.makeText(getApplicationContext(), "im here", LENGTH_SHORT).show();
}

Application opens camera, I can take piture and "accept" it but nothing else happend. Probably application never uses onActivityResult method (I try to write some message in this method but nothing happend).

In AndroidMonitor:

05-09 02:43:50.299 9709-10068/com.hgyghyfghyu.apkana40 W/GooglePlayServicesUtil: Google Play services out of date.  Requires 8115000 but found 5089070
05-09 02:43:50.339 9709-9709/com.hgyghyfghyu.apkana40 W/EGL_emulation: eglSurfaceAttrib not implemented

I use nox app to emulate android phone.

What should I do to solve my problem?

barmi
  • 665
  • 6
  • 22

1 Answers1

2

The selected answer here addresses your problem exactly. You also need to check this answer which addresses the question of where the onActivityResult should be handled. My suggestion is that you change your code as follows:

@Override
public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) {
   super.onViewCreated(view, savedInstanceState);
   view.findViewById(R.id.takephotobtn).setOnClickListener(new  View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                //changed getActivity().startActivityForResult(i, REQUESTCODE); to:
                startActivityForResult(i, REQUESTCODE);
            }
        });

        String title = getArguments().getString("txt", "txt");
        getDialog().setTitle(title);
        getDialog().getWindow().setSoftInputMode(
        WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
    }

onActivityResult() will be invoked first on the Activity. After that it will be reached out to the Fragments, if you call super.onActivityResult() in your Activity. So you need to add the onActivityResult callback in your Activity - so that the one in your Fragment can be invoked.

public void onActivityResult(int requestCode, int resultCode, Intent data) {
   super.onActivityResult(requestCode, resultCode, data);
}

Once that is done, the onActivityResult on your Fragment will be invoked and your code will be executed as you expect.

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode==REQUESTCODE & resultCode== Activity.RESULT_OK){
        Bundle bundle = new Bundle();
        bundle = data.getExtras();
        Bitmap bitmap = (Bitmap)bundle.get("data");
        ImageView img = (ImageView) getActivity().findViewById(R.id.trainersettingsmainicon);
        img.setImageBitmap(bitmap);
    }
}

You need to look at these two closely related

Community
  • 1
  • 1
ishmaelMakitla
  • 3,784
  • 3
  • 26
  • 32
  • So I should use super.super.onActivityResult(requestCode, resultCode, data); in `SettingsBackImageDialog` and onActivityResult with if() in MainActivity? – barmi May 08 '16 at 19:42
  • NO, the other way around, you should call `super.onActivityResult...` in your MainActivity's onActivityResult - then the code you already have in your Fragment's onActivityResult is good - no need to change/move it. – ishmaelMakitla May 08 '16 at 19:48
  • It doesn't work (onActivityResult in `SettingsBackImageDialog` is still not used). Is that any problem that this is DialogFragment, not just Fragment? – barmi May 08 '16 at 19:57
  • Can you please post the latest version of your code - this will help me see which changes you have made or if you made all the necessary changes. For instance, did you call `startActivityForResult(i, REQUESTCODE);` in your Fragment - or did you call `getActivity().startActivityForResult(i, REQUESTCODE);`. – ishmaelMakitla May 08 '16 at 20:09
  • Can you change the MainActivity onActivityResults and explicitly call the onActivityResult on your Fragment like this: `Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.your-fragment-id); fragment.onActivityResult(requestCode, resultCode, data);` – ishmaelMakitla May 08 '16 at 20:33
  • Do you mean `SettingsBackImageDialog` or `TrainerSettings` – barmi May 08 '16 at 20:37
  • Which one has the code for `onActivityResult`? That's the one. – ishmaelMakitla May 08 '16 at 20:41
  • Should I use id like [there](http://stackoverflow.com/questions/9363072/android-set-fragment-id) (FrameLayout id from mainActivity xml?) – barmi May 08 '16 at 20:53
  • Yes, please try that and let;s see if it works. Also, please add Log statement in your MainActivity's onActivityResults - just to see if it gets invoked at least. – ishmaelMakitla May 08 '16 at 20:59
  • I put in my post what actually is in MainActivity. If i click on buton there is toast "im here" but the picture doesn't change. – barmi May 08 '16 at 21:08
  • This is a good sign - now in your Fragment's onActivityResult - can you please add a log statement there as well - before the IF block - just so we are sure that this one too gets called -if this is the case, then perhaps we should review the IF block. – ishmaelMakitla May 08 '16 at 21:18
  • This is strange. Ok, by the way, your `MainActivity` does it extend `FragmentActivity`? Also, the code that handles your onActivityResult does not see to "depend" on the Fragment, so perhaps you should move the code to the MainActivity's onActivityResult. – ishmaelMakitla May 08 '16 at 21:42
  • `MainActivity` does't, `TrainerSettings` extends `Fragment` and `SettingsBackImageDialog` extends `DialogFragment`. So remove onActivityResult() from `SettingsBackImageDialog` and put this code in onActivityResult() in `MainActivity`? – barmi May 08 '16 at 21:50
  • You can try first to extend `FragmentActivity` in your MainActivity - if this does not work, you can then try and move the onActivityResult code - but in this case you change `ImageView img = (ImageView) getActivity().findViewById(R.id.trainersettingsmainicon);` to this: `ImageView img = (ImageView) findViewById(R.id.trainersettingsmainicon);` – ishmaelMakitla May 08 '16 at 21:57
  • I use extends` AppCompatActivity` in `MainActivity`. If I put code from onActivityResult (in `SettingsBackImageDialog`) to the same method in `MainActivity` . There is problem in `if()`, I have `requestCode==REQUESTCODE`, `REQUESTCODE` is 1 but `requestCode` is not const: for example is '131073' or '196609' or... – barmi May 08 '16 at 22:06