tur11ng
tur11ng

Reputation: 1112

Proper way of communication between fragments

Is this a proper way of communication between fragments ?

public class MainActivity extends AppCompatActivity implements IFragmentsHandler {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //...
    }

    @Override 
    protected void startFragment1() {
        Fragment1 f1 = new Fragment1();
        f1.setFragmentsHandler(this);
        getSupportFragmentManager().beginTransaction()
            .replace(R.id.fragment_container, f1)
                .commit();
    }

    @Override
    protected void startFramgment2() {
        Fragment1 f2 = new Fragment1();
        f2.setFragmentsHandler(this);
        getSupportFragmentManager().beginTransaction()
            .replace(R.id.fragment_container, f2)
                .commit();            
    }
}

public class Fragment1 {
    private IFragmentsHadnler fragmentsHandler;

    @Override
    public View onCreateView(LayoutInflater inflater,
        ViewGroup container, Bundle savedInstanceState) {

        final View view = inflater.inflate(R.layout.fragment1, container, false);
        //...Code...
        fragmentsHandler.startFragment1();
    }

    public void setFragmentsHandler(IFragmentsHandler fragmentsHandler) {
        this.fragmentsHandler = fragmentsHandler;
    }
}

public class Fragment2 {
    private IFragmentsHadnler fragmentsHandler;

    @Override
    public View onCreateView(LayoutInflater inflater,
        ViewGroup container, Bundle savedInstanceState) {

        final View view = inflater.inflate(R.layout.fragment2, container, false);
        //...Code...
        fragmentsHandler.startFragment2();
    }

    public void setFragmentsHandler(IFragmentsHandler fragmentsHandler) {
        this.fragmentsHandler = fragmentsHandler;
    }
}

[EDIT1] : Posted the Interface (though it was obvious)

public interface IFragmentsHandler {
    public void startFragment1();
    public void startFragment2();
}

From my Java perspective this will throw OutOfMemoryError but I'm not if it is the same for the Android. Anyway what is the preferred way of communication between fragments?

Upvotes: 0

Views: 326

Answers (3)

Chakir
Chakir

Reputation: 478

As you started, you can use interfaces to communicate between Fragments as suggested by Google.

But an easy way to communicate between fragments is by using event bus (which implements the publish/subscribe pattern) like EventBus library.

You can also use RxJava to create your own event bus and thus make communications between components of your app (have a look to this Stackoverflow question: RxJava as event bus?)

Upvotes: 0

jorgeavilae
jorgeavilae

Reputation: 208

According to android developer guide, communication between fragments is done through the associated Activity.

A fragment use his interface to communicate with the Activity. And the Activity deliver a message by capturing the Fragment instance with findFragmentById() or creating one if needed, then directly call the other fragment's public methods.

  • Fragment1 wants to pass some data: uses his interface method implemented by Activity.
  • Activity executes that method receiving the data, create&replace or find (depending on your layout) the Fragment2 and pass this data or execute some public method on fragment2 class (depending on your layout).
  • Fragment2 extract data from bundle or execute (depending on your layout) his public method to receive the data.

I think the problem in your code is you are misunderstanding interface purpose. You are using for start the same fragment who is calling the method. Fragment1 is calling startFragment1() in his onCreateView(), but it is already started.

If you needed, in here there is a good tutorial.

Upvotes: 1

DmitrySamoylenko
DmitrySamoylenko

Reputation: 1

To communicate between components consider app architecture MVP, VIPER, etc. On code side it may use event bus for communication or just plain callbacks.

  • Do navigation in one place
  • Do business logic in another place
  • Do present-view logic in presenter
  • Do view logic in views, fragments, adapters

Upvotes: 0

Related Questions