(Android) GetActivity() in Fragment.StartActivityForResult to pass values to Activity

Asked 2 years ago, Updated 2 years ago, 121 views

I put a fragment called B on top of the activity called A.

In the fragment B

getActivity().Use startActivityForResult()

B(fragment)->A(activity)->B(fragment) again.

There is a problem here, and activity A receives three factors: int requestCode, int resultCode, and intent data.

If you take a log, The request code has a good value, but Intent has a null value.

I would appreciate it if you could tell me why and how to get the data.

Masters, please. Below is an example code.

B fragment

Intent takePictureIntent = newIntent(MediaStore.ACTION_IMAGE_CAPTURE); // Invoke Photo Intet
                    if (takePictureIntent.resolveActivity(activity.getPackageManager()) != null) {
                        File photoFile = null;
                        try {
                            photoFile = createImageFile();
                        } } catch (IOException e) {
                            e.printStackTrace();
                        }
                        if (photoFile != null) {
                            Uri providerURI = FileProvider.getUriForFile(getContext(), BuildConfig.APPLICATION_ID + ".fileprovider", photoFile); //create a provider
                            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, providerURI); // Recall photo storage path

                            getActivity().startActivityForResult (takePictureIntent, REQUEST_TAKE_PHOTO); // Save Activity Photo
                        } } else {
                            Toast.makeText(getContext(), "Storage is inaccessible.", Toast.LENGTH_SHORT).show();
                            return;
                        }
                    }

A activity

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

    Log.v("resultCode",resultCode+"");
    Log.v("requsetCode",requestCode+"");
    Log.v("data",data+"");

    changeFragment(ReceptionTakePictureViewFragment.class);
}

My goal is to get B Fragment's provider URI from A activity.

android fragment activity

2022-09-22 11:12

2 Answers

First of all, I don't know if I understand the question well. If I misunderstood the problem, please let me know the address of GitHub

A activity

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

Log.v("resultCode",resultCode+"");
Log.v("requsetCode",requestCode+"");
Log.v("data",data+"");

changeFragment(ReceptionTakePictureViewFragment.class);
}

In this function, data is an int object.

Uri providerURI = FileProvider.getUriForFile(getContext(), BuildConfig.APPLICATION_ID + ".fileprovider", photoFile); //create a provider
                        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, providerURI); // Recall photo storage path

                        getActivity().startActivityForResult (takePictureIntent, REQUEST_TAKE_PHOTO); // Save Activity Photo

If you look at this part, you can see that the putExtra function saved data inside the int. If you saved the value using the putExtra function, you can use getTypeExtra. I don't know what form Uri is saved in

 String key = data.getStringExtra(MediaStore.EXTRA_OUTPUT);
//You can use the getStringExtra, getIntExtra functions depending on the type of value you store as putExtra.

Once you add the code like that, the uri will be stored in the variable key. The value stored as String can be changed using the parse function.

URI uri;
uri=URI.parse(key);

I am also a student who is just studying Android and I get a lot of help from the hash code. I could be wrong. I hope it's helpful.


2022-09-22 11:12

This is a callback listener.

public interface CallbackListener {
    void callbackMethod(Uri uri);
}
public class HostActivity extends Activity implements CallbackListener {

    ....

    @Override
    public void callbackMethod(Uri uri) {
        // Value passed by Fragment enters uri factor
    }

    ....
}
public class ChildFragment extends Fragment {
    private CallbackListener mListener;

    @Override
    public void onAttach(Context context) {
        mListener = (CallbackListener) context;
    }

    private void sendValue(Uri value) {
        if (mListener != null) {
            mListener.callbackMethod(value);
        }
    }

}


2022-09-22 11:12

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.