Issue
Bank app:
I have a NavigationDrawerActivity which has more fragments. Each fragment is created after menu item is clicked.
@Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.nav_home) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new HomeFragment()).commit();
} else if (id == R.id.nav_transfer) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, 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() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String accountId = allAccountsList.get(position);
//PASS accountId back to TransferFragment
finish();
}
});
}
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?
Solution
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.
Answered By - Ben Shmuel
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.