Skip to content
This repository has been archived by the owner on Aug 9, 2020. It is now read-only.

Commit

Permalink
Fix #4: Support nested fragments
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor committed May 6, 2016
1 parent 46a5a96 commit 3ac7dc0
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 19 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ allprojects {
And add next dependencies in the build.gradle of the module:
```gradle
dependencies {
compile "com.github.VictorAlbertos:RxActivityResult:0.3.0"
compile "com.github.VictorAlbertos:RxActivityResult:0.3.1"
compile "io.reactivex:rxjava:1.1.0"
}
```
Expand Down
11 changes: 11 additions & 0 deletions app/src/androidTest/java/app/ApplicationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ public class ApplicationTest {
cancelUserAction();
}

@Test public void CheckNestedFragment() {
onView(withId(R.id.bt_fragment_nested)).perform(click());
takePhoto();
checkIntentSender();
}

@Test public void CheckNestedFragmentCancelUserAction() {
onView(withId(R.id.bt_fragment_nested)).perform(click());
cancelUserAction();
}

private void takePhoto() {
onView(withId(R.id.bt_camera)).perform(click());
waitTime();
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
</intent-filter>
</activity>

<activity
android:name="app.HostActivitySampleFragmentNested"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar" />

<activity
android:name="app.HostActivitySampleFragment"
android:label="@string/app_name"
Expand Down
15 changes: 13 additions & 2 deletions app/src/main/java/app/HostActivitySampleFragmentNested.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
package app;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import io.victoralbertos.app.R;

/**
* Created by victor on 06/05/16.
*/
public class HostActivitySampleFragmentNested {
}

public class HostActivitySampleFragmentNested extends AppCompatActivity {

@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.host_activity_sample_fragment_nested);
}
}
2 changes: 0 additions & 2 deletions app/src/main/java/app/SampleParentFragmentNested.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ private void printUserCanceled() {
Toast.makeText(getActivity(), getString(R.string.user_canceled_action), Toast.LENGTH_LONG).show();
}
}


}


4 changes: 4 additions & 0 deletions app/src/main/java/app/StartActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,9 @@ public class StartActivity extends AppCompatActivity {
findViewById(R.id.bt_fragment).setOnClickListener(view -> {
startActivity(new Intent(StartActivity.this, HostActivitySampleFragment.class));
});

findViewById(R.id.bt_fragment_nested).setOnClickListener(view -> {
startActivity(new Intent(StartActivity.this, HostActivitySampleFragmentNested.class));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
android:id="@+id/sample_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="app.SampleFragment"
class="app.SampleParentFragmentNested"
tools:layout="@layout/sample_layout" />

</LinearLayout>
12 changes: 10 additions & 2 deletions app/src/main/res/layout/sample_parent_fragment_nested.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fl_fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent">

</FrameLayout>

</LinearLayout>
6 changes: 6 additions & 0 deletions app/src/main/res/layout/start_activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,10 @@
android:layout_height="wrap_content"
android:text="@string/using_fragment" />

<Button
android:id="@+id/bt_fragment_nested"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/using_nested_fragment" />

</LinearLayout>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
<string name="user_canceled_action">User canceled action</string>
<string name="using_activity">Using activity</string>
<string name="using_fragment">Using fragment</string>
<string name="using_nested_fragment">Using nested fragment</string>
<string name="intent_sender">Intent sender</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -123,22 +123,32 @@ private OnResult onResultFragment() {
FragmentActivity fragmentActivity = (FragmentActivity) activity;
FragmentManager fragmentManager = fragmentActivity.getSupportFragmentManager();

List<Fragment> fragments = fragmentManager.getFragments();

if(fragments != null) {
for(Fragment fragment : fragments){
if(fragment != null && fragment.isVisible() && fragment.getClass() == clazz) {
subscriber.onNext(new Result<T>((T) fragment, resultCode, data));
subscriber.onCompleted();
return;
}
}
Fragment targetFragment = getTargetFragment(fragmentManager.getFragments());

if(targetFragment != null) {
subscriber.onNext(new Result<T>((T) targetFragment, resultCode, data));
subscriber.onCompleted();
}

//If code reaches this point it means some other activity has been stacked as a secondary process.
//Wait until the current activity be the target activity to get the associated fragment
//Do nothing until the current activity be the target activity to get the associated fragment
}
};
}

@Nullable private Fragment getTargetFragment(List<Fragment> fragments) {
if (fragments == null) return null;

for (Fragment fragment : fragments) {
if(fragment != null && fragment.isVisible() && fragment.getClass() == clazz) {
return fragment;
} else if (fragment.getChildFragmentManager() != null) {
List<Fragment> childFragments = fragment.getChildFragmentManager().getFragments();
return getTargetFragment(childFragments);
}
}

return null;
}
}
}

0 comments on commit 3ac7dc0

Please sign in to comment.