-2

I cannot seem to find this anywhere. Here is the issue:

I have a Fragment A that comes from Activity A. This fragment launch Activity B.

I want to send From Activity B to Fragment A. How is that done?

mskw
  • 10,063
  • 9
  • 42
  • 64
  • 1
    send what? what have you tried, so far? BroadcastReciver? Service? custom Application class? why don't you return data from Activity B to A and then let the Activity A send it to Fragment A ... – Selvin Aug 28 '14 at 15:09
  • I was thinking of doing that, is there a more direct solution. The problem is that the activity A could be destroyed after launching activity B – mskw Aug 28 '14 at 15:10
  • I tried using persistence where setting a Boolean in the bundle. But it seems like a hack – mskw Aug 28 '14 at 15:11
  • yeap it can ... then save somewhere in Activity B and reaload Fragment A in onStart of Activity B ... still i don't have such problems because most of time i'm using own ContentProviders to store data ... so if i save something somewhere in other app's component all others components(using the same data) will know about it because of ContentObserver stuff ... – Selvin Aug 28 '14 at 15:11

1 Answers1

2

You can try to use 'startActivityForResult()'. This will start the activity, and then when it's done, it will allow you to return some data to a callback function in the fragment that you override. Here's an example:

private void ActivityBStarter(){
    Intent i = new Intent(getApplicationContext(),ActivityB.class);
    startActivtyForResult(i);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
    //use data.get...  to get data from the activity.
    //This is only called when the activity completes.
}

Then in the activity, do this when you want to finish the activity and pass the result back:

Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
setResult(RESULT_OK,returnIntent); //RESULT_OK can really be any int you want.
finish();

Alternatively, you could use Messengers and message passing to pass data between your Activity and fragment as ActivityB is running, but that's usually not very useful unless you're using a Service.

Here's a link: Example: Communication between Activity and Service using Messaging

Good luck!

Community
  • 1
  • 1
martin_xs6
  • 73
  • 8