1

I have trouble to return data from fragment to activity.

I have Activity A: is main activity for add product, user when click the text view (selectCategory) this activity show to user activity B which content fragment Bb(all type of Category), when user clicks in some of this categories, the app going to show user activity C which has fragment Cc (subcategories).

Here my problem: I need when the user clicks in some of this subcategory which content in fragment Cc to return data to activity A then show to the user.

A call --> B

categoryTextView.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
    Intent i= new Intent(A.this,B.class);
    i.putExtra("fragmenttype","CategoryFragment");
    startActivityForResult(i,10);
  }
}); 

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  if (requestCode == 10) {
    if (resultCode == RESULT_OK) {
      titleProduct.setText(data.getStringExtra("nodecategory"));
    }
  }
}

return data from Cc fragment to A but nothing happened (I need from code to go to activity A to catch data in onActivityResult):

@Override
public void onClick(TreeNode node, Object value) {
  Intent i = new Intent();
  i.putExtra("nodecategory","hi");
  getActivity().setResult(Activity.RESULT_OK, i);
  getActivity().finish();
}
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
MohammadTofi
  • 365
  • 1
  • 7
  • 22
  • Something like this? http://stackoverflow.com/questions/14439941/passing-data-between-fragments-to-activity or this http://stackoverflow.com/questions/9343241/passing-data-between-a-fragment-and-its-container-activity – OneCricketeer Aug 20 '16 at 00:07
  • it's different my friend in the first one he tries to move data from fragment to activity directly it's easy, in my question I have multi-layer (fragment and activity between main Activity A and last fragment Cc ), and when I try to return data activity A not call. – MohammadTofi Aug 20 '16 at 00:16

2 Answers2

0

I tend to rely heavily on EventBus on handling some case like yours. Using EventBus makes our life simpler. We don't need to clutter our code with interface here and there.

A ---> B

First, A will launch B with:

Intent i= new Intent(A.this, B.class);
i.putExtra("fragmenttype","CategoryFragment");
startActivity((i); // No need using startActivityForResult, 
                   // instead receive it from Event generated by B.class.

Then in B, send Event whenever there is something that A need to catch:

EventBus.getDefault().post(new MyEvent("passed_data"));
// You can finish B activity if its job finished.
finish();

With MyEvent class like:

public class MyEvent {
  private String data;

  public MyEvent(String data) {
    this.data = data;
  }
  public String getData() {
    return data;
  }
}

In A, receive the Event:

@Subscribe
public void onEventMessage(MyEvent event) {
  // process event here
  mTextView.setText(event.getData);
}

For C --> A

Consider that we have show C activity. Whenever we want to inform A that something need to do based on C activities (something we do), we just need to fire Event with the data:

EventBus.getDefault().post(new DataFromCEvent("your_c_data"));

with DataFromCEvent like:

public class DataFromCEvent {
  private String data;

  public DataFromCEvent(String data) {
    this.data = data;
  }
  public String getData() {
    return data;
  }
}

In A, you can receive it by adding:

@Subscribe
public void onEventMessage(DataFromCEvent event) {
  // process event here
  mTextView.setText(event.getData);
}

This also works with fragment. We only need to fire an Event for some changes that we need in other activities.

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
0

use Intent.FLAG_ACTIVITY_FORWARD_RESULT on your activity B.

Ex-> ActivityA -> ActivityB -> ActivityC - > ActivityA

ActivityA:

Intent intentB = new Intent(this, ActivityB.class);
startActivityForResult(intentB, requestCode);

ActivityB: activity B will forward to

Intent intentC = new Intent(this, ActivityC.class);
intentC.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
startActivity(intentC);
finish();

ActivityC:

setResult(resultCode);
finish();
rahul
  • 208
  • 1
  • 7