Passing data from Fragment to new Activity

I am trying to pass data from my Main Activity, to my Camera Activity, to my Camera Fragment, and then back to my Main Activity. I want the user to be able to save the information they input on the Main Activity to remain when they are finished using the camera.

On my Main Activity, I am passing the following as intent extras to the Camera Activity:

(NOTE: this works)

Intent cameraIntent = new Intent(MainActivity.this, CameraActivity.class);
    cameraIntent.putExtra("description" , editTextDescription.getText().toString());
    cameraIntent.putExtra("category" , editTextCategory.getText().toString());
    cameraIntent.putExtra("notes" , editTextNotes.getText().toString());
MainActivity.this.startActivity(cameraIntent);

I am then receiving the extras on my Camera Activity successfully, and passing them in a bundle to my Camera Fragment:

(NOTE: this works)

Intent intent = getIntent();

if (intent.getExtras() != null) {

     description = intent.getExtras().getString("description");
     category = intent.getExtras().getString("category");
     notes = intent.getExtras().getString("notes");

    }

if (null == savedInstanceState) {

    CameraFragment cameraFragment = new CameraFragment();

    Bundle bundle = new Bundle();

    bundle.putString("description", description);
    bundle.putString("category", category);
    bundle.putString("notes", notes);
    cameraFragment.setArguments(bundle);

    Log.v(TAG, "Here is the bundle: " + bundle.toString());

    getFragmentManager().beginTransaction()
        .replace(R.id.container, cameraFragment.newInstance())
        .add(cameraFragment, bundle.toString())
        .commit();

    }

In my Camera Fragment, I am receiving the bundle in my onCreateView as follows:

(NOTE: this works)

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    if (getArguments() != null) {

        description = getArguments().getString("description");
        category = getArguments().getString("category");
        notes = getArguments().getString("notes");
        Log.v(TAG, "Here are your arguments from the camera activity:  " + description + "  " + category + "  " + notes);

    } 

    else {
        Log.v(TAG, "Your arguments are null");
    }

    return inflater.inflate(R.layout.fragment_camera, container, false);
}

All of these transactions are properly executing, and I have received all of the information correctly up to this point. My goal now is to take the information, and pass it back to the Main Activity.

ISSUE**

case R.id.doneButton: {

    Intent intent = new Intent(getActivity(), MainActivity.class);
            intent.putExtra("description", description);
            intent.putExtra("category", category);
            intent.putExtra("notes", notes);
            Log.v(TAG, "Here are the extras going back to the Main Activity " + description + "  " + category + "  " + notes);
    startActivity(intent);
            break;

        }

However, I am unable to take the information I have received in my Fragment, and use the variables elsewhere. I have attempted trying to get the arguments in a method other than the onCreateView, and then use the method before passing the extras back to the Main Activity. I have also tried getting the arguments within my onClick switch, case method, however, when I try to send the data back to my Main Activity, it always returns null because it is not taking what it has received in the arguments, but instead using the Strings I have declared earlier.

private String description;
private String category;
private String notes;

Is there a way to take the data I have received in arguments in my Camera Fragment, and use them outside of the onCreateView method, and send them back to my Main Activity?

Why don’t you just make a ‘get’ method in your Fragment .

cameraFragment.getXXXX();

I have kind of rewritten your code and this works.

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent cameraIntent = new Intent(MainActivity.this, CameraActivity.class);
        cameraIntent.putExtra("description" , "description");
        cameraIntent.putExtra("category" , "category");
        cameraIntent.putExtra("notes" , "notes");
        startActivity(cameraIntent);
    }
}

public class CameraActivity extends Activity{

    private String description,category,notes;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);
        Intent intent = getIntent();

        if (intent.getExtras() != null) {

            description = intent.getExtras().getString("description");
            category = intent.getExtras().getString("category");
            notes = intent.getExtras().getString("notes");

        }

        if (null == savedInstanceState) {

            CameraFragment cameraFragment = new CameraFragment();

            Bundle bundle = new Bundle();

            bundle.putString("description", description);
            bundle.putString("category", category);
            bundle.putString("notes", notes);
            cameraFragment.setArguments(bundle);

            Log.v(TAG, "Here is the bundle: " + bundle.toString());

            getFragmentManager().beginTransaction()
                    .replace(R.id.container, cameraFragment)
                    .commit();

        }
    }
}

Even in the onClick of the doneButton the 3 strings are getting populated

public class CameraFragment extends Fragment{

    private String description,category,notes;
    private Button doneButton;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_camera, container, false);
        if (getArguments() != null) {

            description = getArguments().getString("description");
            category = getArguments().getString("category");
            notes = getArguments().getString("notes");
            Log.v("CameraFragment", "Here are your arguments from the camera activity:  " + description + "  " + category + "  " + notes);

        }

        else {
            Log.v("CameraFragment", "Your arguments are null");
        }
        doneButton = view.findViewById(R.id.doneButton);
        doneButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), MainActivity.class);
                intent.putExtra("description", description);
                intent.putExtra("category", category);
                intent.putExtra("notes", notes);
                Log.v("CameraFragment", "Here are the extras going back to the Main Activity " + description + "  " + category + "  " + notes);
                startActivity(intent);
            }
        });
        return view;
    }

}