1

I have made a post in my activity, and it works well in the first fragment BlankFragment, however, when I tried to replace BlankFragment with BlackFragment2, and do the same subscribe, it can't subscribe anymore, here is the code.

MainActivity:

    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction().add(R.id.fr1, new BlankFragment()).commit();

    button = (Button)findViewById(R.id.btn);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            BusStation.getBus().post(new Message("hellworld"));
        }
    });
    btn2 = (Button)findViewById(R.id.btn2);
    btn2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            FragmentManager fragmentManager1 = getSupportFragmentManager();
            fragmentManager1.beginTransaction().replace(R.id.fr1, new BlankFragment2()).commit();
            BusStation.getBus().post(new Message("zhengzhi zhou"));
        }
    });
}

BlankFragment and BlankFragment2 are using the same code:

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

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

@Subscribe
public void receiveMsg(Message msg){
    textView.setText(msg.getMsg());
}

Can anyone help me with this?

Zhengzhi Zhou
  • 21
  • 1
  • 3
  • 2
    Note that Otto has been discontinued for a couple of years. You may wish to consider using some other form of event bus (greenrobot's EventBus, `LocalBroadcastManager`, something based off of RxJava, `MutableLiveData`, etc.). – CommonsWare Sep 07 '17 at 18:47
  • 2
    `FragmentTransaction`s are executed asynchronously by default. That `BlankFragment2` instance likely hasn't initialized and registered by the time you post that message. You can test this by calling `fragmentManager1.executePendingTransactions();` right after the `commit()`. – Mike M. Sep 07 '17 at 18:50
  • @Mike M, Hi, Mike, I tried to add the statement like: fragmentManager1.beginTransaction().replace(R.id.fr1, new BlankFragment2()).commit(); fragmentManager1.executePendingTransactions(); BusStation.getBus().post(new Message("zhengzhi zhou")); But the app clashed, with a log "java.lang.RuntimeException: Could not dispatch event: class com.example.administrator.myapplication.Message to handler " at the line of BusStation.getBus().post(new Message("zhengzhi zhou")); – Zhengzhi Zhou Sep 07 '17 at 19:07
  • Look further on in the stack trace. There's likely another Exception with the specific cause. – Mike M. Sep 07 '17 at 19:13
  • @MikeM, the Exception is "java.lang.RuntimeException: Could not dispatch event: class com.example.administrator.myapplication.Message to handler [EventHandler public void com.example.administrator.myapplication.BlankFragment2.receiveMsg(com.example.administrator.myapplication.Message)]: null" – Zhengzhi Zhou Sep 07 '17 at 19:19
  • Well, I've never used Otto, but it looks like maybe your `receiveMsg()` method is throwing an Exception. Are you sure `textView` isn't null? There should be another specific Exception in the stack trace after the `RuntimeException`. – Mike M. Sep 07 '17 at 19:25
  • 1
    @MikeM, oh......thank you, I write down the wrong id......it works now, thanks a lot – Zhengzhi Zhou Sep 07 '17 at 19:33

1 Answers1

0

Use commitNow() instead Of commit()

 FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        // Replace the contents of the container with the new fragment
        ft.replace(R.id.fragment_place, fragment);
        // or ft.add(R.id.your_placeholder, new FooFragment());
        // Complete the changes added above
        ft.commitNow();
MrZ
  • 560
  • 2
  • 12