This repository has been archived by the owner on Aug 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
provide a safety instance of the Activity/Fragment to stay sync with …
…the most recent instance
- Loading branch information
Victor
committed
Mar 16, 2016
1 parent
9c37211
commit 3ca2fbb
Showing
21 changed files
with
347 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package app; | ||
|
||
import android.os.Bundle; | ||
import android.support.v7.app.AppCompatActivity; | ||
|
||
import io.victoralbertos.app.R; | ||
|
||
public class HostActivitySampleFragment extends AppCompatActivity { | ||
|
||
@Override protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.host_activity_sample_fragment); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package app; | ||
|
||
import android.app.Application; | ||
|
||
import rx_activity_result.RxActivityResult; | ||
|
||
public class SampleApp extends Application { | ||
|
||
@Override public void onCreate() { | ||
super.onCreate(); | ||
RxActivityResult.register(this); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package app; | ||
|
||
import android.app.Activity; | ||
import android.content.Intent; | ||
import android.graphics.Bitmap; | ||
import android.os.Bundle; | ||
import android.provider.MediaStore; | ||
import android.support.v4.app.Fragment; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
import android.widget.ImageView; | ||
import android.widget.Toast; | ||
|
||
import io.victoralbertos.app.R; | ||
import rx_activity_result.RxActivityResult; | ||
|
||
public class SampleFragment extends Fragment { | ||
|
||
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | ||
View view = inflater.inflate(R.layout.sample_layout, container, false); | ||
return view; | ||
} | ||
|
||
@Override public void onActivityCreated(Bundle savedInstanceState) { | ||
super.onActivityCreated(savedInstanceState); | ||
getView().findViewById(R.id.bt_camera).setOnClickListener(view -> camera()); | ||
} | ||
|
||
private void camera() { | ||
Intent takePhoto = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); | ||
|
||
RxActivityResult.on(this).startIntent(takePhoto) | ||
.subscribe(result -> { | ||
Intent data = result.data(); | ||
int resultCode = result.resultCode(); | ||
|
||
if (resultCode == Activity.RESULT_OK) { | ||
result.targetUI().showImage(data); | ||
} else { | ||
result.targetUI().printUserCanceled(); | ||
} | ||
}); | ||
} | ||
|
||
private void showImage(Intent data) { | ||
Bitmap imageBitmap = (Bitmap) data.getExtras().get("data"); | ||
((ImageView) getView().findViewById(R.id.iv_thumbnail)).setImageBitmap(imageBitmap); | ||
} | ||
|
||
private void printUserCanceled() { | ||
Toast.makeText(getActivity(), getString(R.string.user_canceled_action), Toast.LENGTH_LONG).show(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package app; | ||
|
||
import android.content.Intent; | ||
import android.os.Bundle; | ||
import android.support.v7.app.AppCompatActivity; | ||
|
||
import io.victoralbertos.app.R; | ||
|
||
public class StartActivity extends AppCompatActivity { | ||
@Override protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.start_activity); | ||
|
||
findViewById(R.id.bt_activity).setOnClickListener(view -> { | ||
startActivity(new Intent(StartActivity.this, SampleActivity.class)); | ||
}); | ||
|
||
findViewById(R.id.bt_fragment).setOnClickListener(view -> { | ||
startActivity(new Intent(StartActivity.this, HostActivitySampleFragment.class)); | ||
}); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:orientation="vertical" android:layout_width="match_parent" | ||
android:layout_height="match_parent"> | ||
|
||
<fragment | ||
android:id="@+id/sample_fragment" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
class="app.SampleFragment" | ||
tools:layout="@layout/sample_layout" /> | ||
|
||
</LinearLayout> |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:orientation="vertical"> | ||
|
||
<Button | ||
android:id="@+id/bt_activity" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:text="@string/using_activity" /> | ||
|
||
<Button | ||
android:id="@+id/bt_fragment" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:text="@string/using_fragment" /> | ||
|
||
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
<resources> | ||
<!-- Example customization of dimensions originally defined in res/values/dimens.xml | ||
(such as screen margins) for screens with more than 820dp of available width. This | ||
(such as screen margins) for screens startIntent more than 820dp of available width. This | ||
would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively). --> | ||
<dimen name="activity_horizontal_margin">64dp</dimen> | ||
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
rx_activity_result/src/main/java/rx_activity_result/ActivitiesLifecycleCallbacks.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package rx_activity_result; | ||
|
||
import android.app.Activity; | ||
import android.app.Application; | ||
import android.os.Bundle; | ||
|
||
class ActivitiesLifecycleCallbacks { | ||
private final Application application; | ||
private Activity liveActivityOrNull; | ||
private Application.ActivityLifecycleCallbacks activityLifecycleCallbacks; | ||
|
||
public ActivitiesLifecycleCallbacks(Application application) { | ||
this.application = application; | ||
registerActivityLifeCycle(); | ||
} | ||
|
||
private void registerActivityLifeCycle() { | ||
if (activityLifecycleCallbacks != null) application.unregisterActivityLifecycleCallbacks(activityLifecycleCallbacks); | ||
|
||
activityLifecycleCallbacks = new Application.ActivityLifecycleCallbacks() { | ||
@Override public void onActivityCreated(Activity activity, Bundle savedInstanceState) { | ||
liveActivityOrNull = activity; | ||
} | ||
|
||
@Override public void onActivityStarted(Activity activity) {} | ||
|
||
@Override public void onActivityResumed(Activity activity) { | ||
liveActivityOrNull = activity; | ||
} | ||
|
||
@Override public void onActivityPaused(Activity activity) { | ||
liveActivityOrNull = null; | ||
} | ||
|
||
@Override public void onActivityStopped(Activity activity) {} | ||
|
||
@Override public void onActivitySaveInstanceState(Activity activity, Bundle outState) {} | ||
|
||
@Override public void onActivityDestroyed(Activity activity) {} | ||
}; | ||
|
||
application.registerActivityLifecycleCallbacks(activityLifecycleCallbacks); | ||
} | ||
|
||
Activity getLiveActivity() { | ||
return liveActivityOrNull; | ||
} | ||
} |
Oops, something went wrong.