Skip to content
This repository has been archived by the owner on Oct 25, 2022. It is now read-only.

Commit

Permalink
Merge pull request #4 from MRezaNasirloo/bugfix/support_fragment
Browse files Browse the repository at this point in the history
Fixes multiple call to support fragment presenter's callbacks.
  • Loading branch information
MRezaNasirloo authored Apr 25, 2018
2 parents d524bf2 + 917300b commit a9f9962
Showing 1 changed file with 19 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentManager.FragmentLifecycleCallbacks;

Expand All @@ -41,7 +40,9 @@ public class SlickDelegateFragment<V, P extends SlickPresenter<V>> extends Fragm

private P presenter;
private Class cls;
private boolean multiInstance = false;
private boolean onViewUpCalled;
private boolean onViewDownCalled;
private boolean onDestroyCalled;

public SlickDelegateFragment(P presenter, Class cls, int id) {
if (presenter == null) {
Expand All @@ -50,56 +51,46 @@ public SlickDelegateFragment(P presenter, Class cls, int id) {
this.presenter = presenter;
this.cls = cls;
this.id = id;
if (id != -1) multiInstance = true;
}

@Override
@SuppressWarnings("unchecked")
public void onFragmentStarted(FragmentManager fm, Fragment fragment) {
if (multiInstance) {
if (isSameInstance(fragment)) {
presenter.onViewUp((V) fragment);
}

} else if (cls.isInstance(fragment)) {
if (onViewUpCalled) return;
if (cls.isInstance(fragment) && isSameInstance(fragment)) {
presenter.onViewUp((V) fragment);
onViewDownCalled = false;
onViewUpCalled = true;
}
}

@Override
public void onFragmentStopped(FragmentManager fm, Fragment fragment) {
if (multiInstance) {
if (isSameInstance(fragment)) {
presenter.onViewDown();
}
} else if (cls.isInstance(fragment)) {
if (onViewDownCalled) return;
if (cls.isInstance(fragment) && isSameInstance(fragment)) {
presenter.onViewDown();
onViewDownCalled = true;
onViewUpCalled = false;
}
}

@Override
public void onFragmentDestroyed(FragmentManager fm, Fragment fragment) {
if (multiInstance) {
if (isSameInstance(fragment)) {
destroy(fragment);
}
} else if (cls.isInstance(fragment)) {
if (onDestroyCalled) return;
if (cls.isInstance(fragment) && isSameInstance(fragment)) {
destroy(fragment);
}
}

private void destroy(@NonNull Fragment fragment) {
FragmentActivity activity = fragment.getActivity();
if (activity == null || !activity.isChangingConfigurations()) {
FragmentManager fragmentManager = fragment.getFragmentManager();
if (fragmentManager != null) {
fragmentManager.unregisterFragmentLifecycleCallbacks(this);
}
private void destroy(Fragment fragment) {
if (!fragment.getActivity().isChangingConfigurations()) {
fragment.getFragmentManager().unregisterFragmentLifecycleCallbacks(this);
presenter.onDestroy();
if (listener != null) {
listener.onDestroy(id);
}
presenter = null;
onDestroyCalled = true;
}
}

Expand All @@ -126,8 +117,8 @@ public static int getId(@NonNull Object view) {
return -1;
}

private boolean isSameInstance(@NonNull Object view) {
private boolean isSameInstance(Object view) {
final int id = getId(view);
return id != -1 && id == this.id;
return id == this.id;
}
}

0 comments on commit a9f9962

Please sign in to comment.