Pass Data From Activity Back To Already Created Fragment

Bank app:

I have a NavigationDrawerActivity which has more fragments. Each fragment is created after menu item is clicked.

public boolean onNavigationItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == {
        getSupportFragmentManager().beginTransaction().replace(, new HomeFragment()).commit();
    } else if (id == {
        getSupportFragmentManager().beginTransaction().replace(, new TransferFragment()).commit();
    return true;

If I click on "transfer" menu item, TransferFragment is created. This fragment is responsible for simple money transferring from one bank account to another. I have a "choose account" button in this fragment which starts (using intent) new activity - ChooseAccountActivity. This activity is just a listView containing all accounts which can be used to transfer the money from.

After the user clicks on one of the accounts, the accountId (String) should be sent BACK to TransferFragment so the user is able to continue filling up other EditTexts (amount, receiver's account id, etc.)

method in "ChooseAccountActivity":

public void eventAfterClickOnListViewItem() {
    accountsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String accountId = allAccountsList.get(position);
            //PASS accountId back to TransferFragment

So I need to send the data (accountId) back to TransferFragment. But Intent doesn't work because I work with fragment. The TransferFragment is already created so only onStart, onResume,.. is called after coming back to the fragment.

Any tip or advice how I can pass the data from the activity to the already created fragment? Am I doing this right or should I change my thinking?



You can use startActivityForResult in your TransferFragment to start ChooseAccountActivity and override onActivityResult at TransferFragment to get the accountId. Make sure to read this to get the results in your fragment.