3

It's the first time that I'm using this library, but I was following this video tutorial to send data through Fragments, but in my case, it's just Activities.. So this how I did

Activity that I'm sending data :

public void onClick(View view) {
    String passing_data = new Gson().toJson(user);
    BusStation.getBus().post(new MessageData(passing_data));
    Intent intent = new Intent(activity,UserAdsView.class);
    activity.startActivity(intent);
}

BusStation Class :

public class BusStation {
    private static Bus bus = new Bus();

    public static Bus getBus() {
        return bus;
    }
}

MessageData Class :

public class MessageData {
    private String msgData;

    public MessageData(String msgData) {
        this.msgData = msgData;
    }

    public String getMsgData() {
        return msgData;
    }
}

And finally at the UserAdsView Activity :

@Override
protected void onResume() {
    super.onResume();
    BusStation.getBus().register(this);
}

@Override
protected void onPause() {
    super.onPause();
    BusStation.getBus().unregister(this);
}

@Subscribe
public void recievedData(MessageData messageData){
    target = messageData.getMsgData();
    Toast.makeText(getApplicationContext(), target, Toast.LENGTH_SHORT).show();
}

As was mentioned on video, this method recievedData should be fired!

V-rund Puro-hit
  • 5,518
  • 9
  • 31
  • 50

3 Answers3

4

When you send notification in first activity at that time, UserAdsView Activity is not registered hence there are no listeners for events.

At this line

 BusStation.getBus().post(new MessageData(passing_data));

you are sending notification but there is nothing registered to receive this notification. i.e. UserAdsView Activity has not started yet.

If you need to pass data to activity at launch time, simply send it via Intent.

mallaudin
  • 4,744
  • 3
  • 36
  • 68
1

add in file Gradle

dependencies {
    compile 'com.squareup:otto:1.3.8'
}

Create class OttoBus

public class OttoBus {
    private static Bus sBus;
    public static Bus getBus() {
        if (sBus == null)
            sBus = new Bus();
        return sBus;
    }
}

Create Events Class when pass data in android

public class Events {

    public static class FragmentActivityMessage {

        private String message;

        public FragmentActivityMessage(String message) {
            this.message = message;
        }

        public String getMessage() {
            return message;
        }
    }

    public static class ActivityFragmentMessage {

        private String message;

        public ActivityFragmentMessage(String message) {
            this.message = message;
        }

        public String getMessage() {
            return message;
        }
    }

}

function pass data

 public void sendMessageToFragment(View view) {
        EditText etMessage = findViewById(R.id.activityData);
        OttoBus.getBus().post(String.valueOf(etMessage.getText()));
    }

function event getdata

@Subscribe
    public void getMessage(Events.ActivityFragmentMessage message) {
        TextView messageView = findViewById(R.id.message);
        messageView.setText(message.getMessage());
    }
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Tienanhvn
  • 239
  • 5
  • 9
  • Thanks for that but it should be OttoBus.getBus().post(new Events.ActivityFragmentMessage(String.valueOf(etMessage.getText()))); – Dominique Lorre Aug 20 '19 at 14:09
  • Thanks you , OttoBus.getBus().post(String.valueOf(etMessage.getText())); you can change OttoBus.getBus().post(new Events.ActivityFragmentMessage(String.valueOf(etMessage.getText()))); – Tienanhvn Aug 21 '19 at 06:13
0

You need to make your MessageData object parcelable.

Then in your onClick() :

public void onClick(View view) {
    String passing_data = new Gson().toJson(user);
    Bundle extras = new Bundle();
    extras.putParcelable("key",new MessageData(passing_data));
    Intent intent = new Intent(activity,UserAdsView.class);
    intent.putExtras(extras)
    activity.startActivity(intent);
}

Then in onCreate() of your UserAdsView Activity :

MessageData data = (MessageData)getIntent().getExtras().getParcelable("key");
mallaudin
  • 4,744
  • 3
  • 36
  • 68
nipun.birla
  • 719
  • 5
  • 19