Search code examples
androidandroid-fragmentsandroid-activityandroid-savedstate

Passing String from Activity back to running Fragment in Android


There are a few posts about similar problems but I just don't get the wright answers and I hope that anyone can help me.

Situation: I have a MainActivity that contains several Fragments. In one Fragment I start a CameraActivity with an intent. When the user has taken the picture, the CameraActivity gets closed with finish() and we return back to the previous Fragment.

Goal: I give the Picture a certain name and would like to pass this name from the CameraActivity to the Fragment.

Problem: Even though I call finish() in the CameraActivity and the screen returns back to the previous Fragment, the method onSaveInstanceState(Bundle outstate) is never called. Why is that?

Or how could I solve the problem otherwise?

Here is my code:

public class CameraActivity extends Activity {
    // more code here

    String imageFileName;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // some methods here
        finish(); // finish the current Activity and get back to previous Fragment
    }

    // more code here

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        Log.d(TAG_LOG, "onSaveInstanceState() called"); // is never called!
        outState.putString("imageFileName", imageFileName);
        super.onSaveInstanceState(outState);
    }
}

And in the Fragment I do this:

public class MapFragment extends Fragment implements View.OnClickListener {

    // more code here

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_map, container, false);
        // some methods here

        if (savedInstanceState != null) {
            String imageFileName = getArguments().getString("imageFileName");
            Log.d(TAG_LOG, "image filename: " + imageFileName);
        }

        return view;
    }

    // more code here
}

Any help is highly appreciated!


Solution

  • You should take a look at startActivityForResults. Here is a short example (haven't tried it but you will get the idea) :

    Fragment

    Intent intent = new Intent(MainActivity.this,CameraActivity.class);
    getActivity().startActivityForResult(intent,RESULT_PIC_TAKEN); // you may start an activity from another activity, not a fragment
    

    CameraActivity

    Intent results = new Intent();
    results.putExtra("com;yourpackage.PIC_NAME", picName);
    setResult(CameraActivity.RESULT_OK,results);
    finish();
    

    Back to MainActivity (since you called getActivity())

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    
        if (requestCode == RESULT_PIC_TAKEN) {
            if(resultCode == Activity.RESULT_OK){
                Bundle results = data.getExtras();
                String picName = results.getString("com.yourpackage.PIC_NAME");
                // code, and send what you want to the fragment
            }
        }
    }
    

    Hope this will help you!