0

I have fragment (x) inside FragmentStatePagerAdapter that is inside activity (y) , in fragment (x) i have startActivity to another actvity (z) , how to send listener or callback from activity (z) into activity (y)

Sunny
  • 3,134
  • 1
  • 17
  • 31

2 Answers2

1

You can't. But you can use startActivityForResult to start activity(z). Before activity(z) activity is finishing, you have to set a result. Then you can handle this result in activity(y).

Have a look on this https://developer.android.com/training/basics/intents/result

Maik Peschutter
  • 603
  • 4
  • 9
0

Start your second activity for result

static final int PICK_CONTACT_REQUEST = 1;  // The request code
...
private void pickContact() {
    Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts"));
    pickContactIntent.setType(Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers
    startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
}

Then before finish second activity setup result intent

Intent resultIntent = new Intent()
resultIntent.putExtra("SOME_TAG", SOME RESULT HERE)
activity.setResult(Activity.RESULT_OK, resultIntent);
activity.finish();

Then in first activity this intent will be handled in onActivityResult method

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Check which request we're responding to
    if (requestCode == PICK_CONTACT_REQUEST) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {
            // The user picked a contact.
            // The Intent's data Uri identifies which contact was selected.

            // Do something with the contact here (bigger example below)
        }
    }
}