Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating fragment from Activity Using Rxjava Android

I have a simple use case where:

  • Activity1 create a fragment1

  • fragment1 after creation notify to activity that it is created and update its activity1 views.

  • activity1 after getting notification update fragment1 views.

I am using rxandroid , sublibrary rxlifecycle components and android , but i am still in learning phase , there was not even rx-lifecycle tag on stackoverflow , so i am still struggling to understand the flow of this library..

Edit

I prefer not to use EventBus , it's just like everyone shouting at everyone to do something, so Rxjava Observable approach will be much useful

like image 816
Zulqurnain Jutt Avatar asked Nov 16 '16 16:11

Zulqurnain Jutt


People also ask

How to pass data from one activity to another activity fragment?

Here ViewModel will act as a top governing body which will pass data between fragments and activity. We only need to create ViewModel class and create an instance in the fragment but using the activity scope so that it will be available for all the fragment of the activity including activity itself. Read the above line again.

What is a fragment in Android?

In Android, a fragment is a portion of the user interface that can be used again and again. Fragment manages its own layout and has its own life cycle. Since fragment is a small portion of the bigger user interface, it can only be initialized inside an activity or another fragment.

How to create a ViewModel from a fragment?

Use activity's scope to create an instance of the ViewModel in fragment so that all the fragments and activity now has a common ViewModel and have access to the same ViewModelStore. If the activity is re-created, it and all its fragments receive the same ViewModel instance that was created by the associated activity.

How do I display resources inside a fragment?

Since fragment is a small portion of the bigger user interface, it can only be initialized inside an activity or another fragment. So if we wish to display any type of resources, such as a string, or an image inside the fragment, we will need to declare them in the activity and then pass it to the fragment.


2 Answers

For posting information from fragment to activity, you should use an event bus for informing activity about fragment creation (replacement to the callbacks and the mess they created).

Sample code for event bus with RxJava is:

public class SampleEventsBus {
    private static final String TAG = SampleEventsBus.class.getSimpleName();
    private static final String TAG2 = SampleEventsBus.class.getCanonicalName();

    public static final int ACTION_FRAGMENT_CREATED = 1;
    public static final int ACTION_FRAGMENT_OTHER = 2;

    private static SampleEventsBus mInstance;

    public static SampleEventsBus getInstance() {
        if (mInstance == null) {
            mInstance = new SampleEventsBus();
        }
        return mInstance;
    }

    private SampleEventBus() {}

    private PublishSubject<Integer> fragmentEventSubject = PublishSubject.create();

    public Observable<Integer> getFragmentEventObservable() {
        return fragmentEventSubject;
    }

    public void postFragmentAction(Integer actionId) {
        fragmentEventSubject.onNext(actionId);
    }
}

Then from your fragment you can call:

SampleEventsBus.getInstance().postFragmentAction(SampleEventsBus.ACTION_FRAGMENT_CREATED);

from onAttach() or onViewCreated() or any place you prefer.

Also, in activity you will need to put the following code to listet to your event bus:

SampleEventsBus .getInstance().getFragmentEventObservable().subscribe(new Subscriber<Integer>() {
        @Override
        public void onCompleted() {

        }

        @Override
        public void onError(Throwable e) {

        }

        @Override
        public void onNext(Integer actionId) {
            if(actionId == SampleEventsBus.ACTION_FRAGMENT_CREATED) {
                //do any required action
            }
        }
    });

For the second part, i.e. to update the fragment from activity, I won't recommend using this method as it will lead to unnecessary complexity, Instead use the "original way" as:

  1. Create a method in Fragment as updateView(Object obj)
  2. In onNext(), get the desired fragment as SampleFragment fragment = (SampleFragment)getSupportFragmentManager().findFragmentByTag("TAG");
  3. call fragment.updateView(obj);

Hope this helps.

like image 80
idle_developer Avatar answered Oct 17 '22 19:10

idle_developer


Two points to consider:

  1. Just because you use an EventBus does not mean that it needs to be global. You can have multiple event buses if you want, and you can just share a single one between two components (Activity and Fragment).

  2. There are several examples in the RxJava documentation that show how to implement event bus functionality using RxJava

By Using an event bus, you can simplify things greatly, by disassociating the whole process from the Android lifecycle.

like image 6
GreyBeardedGeek Avatar answered Oct 17 '22 18:10

GreyBeardedGeek