Skip to content

Commit 064ef3f

Browse files
Merge pull request #92 from Omega-R/feature/start_activity_from_fragments
FragmentIntentHandlers have been added
2 parents a593b68 + b0df226 commit 064ef3f

5 files changed

Lines changed: 244 additions & 0 deletions

File tree

core/src/main/java/com/omega_r/libs/omegaintentbuilder/builders/ActivityIntentBuilder.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@
1111
package com.omega_r.libs.omegaintentbuilder.builders
1212

1313
import android.app.Activity
14+
import android.app.Fragment
1415
import android.content.Context
1516
import com.omega_r.libs.omegaintentbuilder.interfaces.IntentHandler
1617
import com.omega_r.libs.omegaintentbuilder.handlers.ActivityIntentHandler
1718
import com.omega_r.libs.omegaintentbuilder.handlers.ContextIntentHandler
19+
import com.omega_r.libs.omegaintentbuilder.handlers.FragmentIntentHandler
20+
import com.omega_r.libs.omegaintentbuilder.handlers.SupportFragmentIntentHandler
1821

1922
class ActivityIntentBuilder<T : Activity>(
2023
private val context: Context,
@@ -29,6 +32,14 @@ class ActivityIntentBuilder<T : Activity>(
2932
return ActivityIntentHandler(activity, createIntent())
3033
}
3134

35+
override fun createIntentHandler(fragment: Fragment): FragmentIntentHandler {
36+
return FragmentIntentHandler(fragment, createIntent())
37+
}
38+
39+
override fun createIntentHandler(fragment: android.support.v4.app.Fragment): SupportFragmentIntentHandler {
40+
return SupportFragmentIntentHandler(fragment, createIntent())
41+
}
42+
3243
override fun startActivity() {
3344
createIntentHandler().startActivity()
3445
}

core/src/main/java/com/omega_r/libs/omegaintentbuilder/builders/BaseActivityBuilder.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@
1111
package com.omega_r.libs.omegaintentbuilder.builders
1212

1313
import android.app.Activity
14+
import android.app.Fragment
1415
import android.content.Context
1516
import com.omega_r.libs.omegaintentbuilder.interfaces.IntentHandler
1617
import com.omega_r.libs.omegaintentbuilder.handlers.ActivityIntentHandler
1718
import com.omega_r.libs.omegaintentbuilder.handlers.ContextIntentHandler
19+
import com.omega_r.libs.omegaintentbuilder.handlers.FragmentIntentHandler
20+
import com.omega_r.libs.omegaintentbuilder.handlers.SupportFragmentIntentHandler
1821

1922
abstract class BaseActivityBuilder(private val context: Context): IntentHandler {
2023

@@ -26,6 +29,14 @@ abstract class BaseActivityBuilder(private val context: Context): IntentHandler
2629
return ActivityIntentHandler(activity, createIntent())
2730
}
2831

32+
override fun createIntentHandler(fragment: Fragment): FragmentIntentHandler {
33+
return FragmentIntentHandler(fragment, createIntent())
34+
}
35+
36+
override fun createIntentHandler(fragment: android.support.v4.app.Fragment): SupportFragmentIntentHandler {
37+
return SupportFragmentIntentHandler(fragment, createIntent())
38+
}
39+
2940
override fun startActivity() {
3041
createIntentHandler().startActivity()
3142
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Copyright (c) 2018 Omega-r
3+
*
4+
* OmegaIntentBuilder
5+
* FragmentIntentHandler.kt
6+
*
7+
* @author: Roman Tcaregorodtcev <roman.tc@omega-r.com>
8+
* Github: R12rus
9+
* Date: October 28, 2018
10+
*/
11+
12+
package com.omega_r.libs.omegaintentbuilder.handlers
13+
14+
import android.app.Fragment
15+
import android.content.ActivityNotFoundException
16+
import android.content.Intent
17+
import android.os.Bundle
18+
19+
/**
20+
* FragmentIntentHandler is a helper for start intents
21+
* Support startActivityForResult
22+
*/
23+
class FragmentIntentHandler(private val fragment: Fragment, private val createdIntent: Intent) : ContextIntentHandler(fragment.activity, createdIntent) {
24+
25+
/**
26+
* Launch an activity for which you would like a result when it finished.
27+
* When this activity exits, your
28+
* onActivityResult() method will be called with the given requestCode.
29+
* Using a negative requestCode is the same as calling
30+
* {@link #android.app.Activity.startActivity} (the activity is not launched as a sub-activity).
31+
*
32+
* <p>Note that this method should only be used with Intent protocols
33+
* that are defined to return a result. In other protocols (such as
34+
* {Intent#ACTION_MAIN} or {@link Intent#ACTION_VIEW}), you may
35+
* not get the result when you expect. For example, if the activity you
36+
* are launching uses {Intent#FLAG_ACTIVITY_NEW_TASK}, it will not
37+
* run in your task and thus you will immediately receive a cancel result.
38+
*
39+
* <p>As a special case, if you call startActivityForResult() with a requestCode
40+
* >= 0 during the initial onCreate(Bundle savedInstanceState)/onResume() of your
41+
* activity, then your window will not be displayed until a result is
42+
* returned back from the started activity. This is to avoid visible
43+
* flickering when redirecting to another activity.
44+
*
45+
* <p>This method throws {@throws android.content.ActivityNotFoundException}
46+
* if there was no Activity found to run the given Intent.
47+
*
48+
* @param createdIntent The createdIntent to start.
49+
* @param requestCode If >= 0, this code will be returned in
50+
* onActivityResult() when the activity exits.
51+
* @param options Additional options for how the Activity should be started.
52+
* See {@link android.content.Context#startActivity(Intent, Bundle)}
53+
* Context.startActivity(Intent, Bundle)} for more details.
54+
*
55+
* @throws android.content.ActivityNotFoundException
56+
*
57+
* @see #startActivity
58+
*/
59+
@JvmOverloads
60+
fun startActivityForResult(requestCode: Int, options: Bundle? = null) {
61+
try {
62+
if (getChooserTitle().isNullOrEmpty()) {
63+
if (options == null) {
64+
fragment.startActivityForResult(createdIntent, requestCode)
65+
} else {
66+
fragment.startActivityForResult(createdIntent, requestCode, options)
67+
}
68+
} else {
69+
if (options == null) {
70+
fragment.startActivityForResult(createChooserIntent(), requestCode)
71+
} else {
72+
fragment.startActivityForResult(createChooserIntent(), requestCode, options)
73+
}
74+
}
75+
} catch (exc: ActivityNotFoundException) {
76+
handleStartActivityException(exc)
77+
}
78+
}
79+
80+
override fun failToast(message: String): FragmentIntentHandler {
81+
super.failToast(message)
82+
return this
83+
}
84+
85+
override fun failToast(message: Int): FragmentIntentHandler {
86+
super.failToast(message)
87+
return this
88+
}
89+
90+
override fun failCallback(failCallback: FailCallback): FragmentIntentHandler {
91+
super.failCallback(failCallback)
92+
return this
93+
}
94+
95+
override fun failIntent(failIntent: Intent): FragmentIntentHandler {
96+
super.failIntent(failIntent)
97+
return this
98+
}
99+
100+
override fun failIntentHandler(failIntentHandler: ContextIntentHandler?): FragmentIntentHandler {
101+
super.failIntentHandler(failIntentHandler)
102+
return this
103+
}
104+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Copyright (c) 2018 Omega-r
3+
*
4+
* OmegaIntentBuilder
5+
* SupportFragmentIntentHandler.kt
6+
*
7+
* @author: Roman Tcaregorodtcev <roman.tc@omega-r.com>
8+
* Github: R12rus
9+
* Date: October 28, 2018
10+
*/
11+
package com.omega_r.libs.omegaintentbuilder.handlers
12+
13+
import android.content.ActivityNotFoundException
14+
import android.content.Intent
15+
import android.os.Bundle
16+
import android.support.v4.app.Fragment
17+
18+
/**
19+
* FragmentIntentHandler is a helper for start intents
20+
* Support startActivityForResult
21+
*/
22+
class SupportFragmentIntentHandler(private val fragment: Fragment, private val createdIntent: Intent) : ContextIntentHandler(fragment.context!!, createdIntent) {
23+
24+
/**
25+
* Launch an activity for which you would like a result when it finished.
26+
* When this activity exits, your
27+
* onActivityResult() method will be called with the given requestCode.
28+
* Using a negative requestCode is the same as calling
29+
* {@link #android.app.Activity.startActivity} (the activity is not launched as a sub-activity).
30+
*
31+
* <p>Note that this method should only be used with Intent protocols
32+
* that are defined to return a result. In other protocols (such as
33+
* {Intent#ACTION_MAIN} or {@link Intent#ACTION_VIEW}), you may
34+
* not get the result when you expect. For example, if the activity you
35+
* are launching uses {Intent#FLAG_ACTIVITY_NEW_TASK}, it will not
36+
* run in your task and thus you will immediately receive a cancel result.
37+
*
38+
* <p>As a special case, if you call startActivityForResult() with a requestCode
39+
* >= 0 during the initial onCreate(Bundle savedInstanceState)/onResume() of your
40+
* activity, then your window will not be displayed until a result is
41+
* returned back from the started activity. This is to avoid visible
42+
* flickering when redirecting to another activity.
43+
*
44+
* <p>This method throws {@throws android.content.ActivityNotFoundException}
45+
* if there was no Activity found to run the given Intent.
46+
*
47+
* @param createdIntent The createdIntent to start.
48+
* @param requestCode If >= 0, this code will be returned in
49+
* onActivityResult() when the activity exits.
50+
* @param options Additional options for how the Activity should be started.
51+
* See {@link android.content.Context#startActivity(Intent, Bundle)}
52+
* Context.startActivity(Intent, Bundle)} for more details.
53+
*
54+
* @throws android.content.ActivityNotFoundException
55+
*
56+
* @see #startActivity
57+
*/
58+
@JvmOverloads
59+
fun startActivityForResult(requestCode: Int, options: Bundle? = null) {
60+
try {
61+
if (getChooserTitle().isNullOrEmpty()) {
62+
if (options == null) {
63+
fragment.startActivityForResult(createdIntent, requestCode)
64+
} else {
65+
fragment.startActivityForResult(createdIntent, requestCode, options)
66+
}
67+
} else {
68+
if (options == null) {
69+
fragment.startActivityForResult(createChooserIntent(), requestCode)
70+
} else {
71+
fragment.startActivityForResult(createChooserIntent(), requestCode, options)
72+
}
73+
}
74+
} catch (exc: ActivityNotFoundException) {
75+
handleStartActivityException(exc)
76+
}
77+
}
78+
79+
override fun failToast(message: String): SupportFragmentIntentHandler {
80+
super.failToast(message)
81+
return this
82+
}
83+
84+
override fun failToast(message: Int): SupportFragmentIntentHandler {
85+
super.failToast(message)
86+
return this
87+
}
88+
89+
override fun failCallback(failCallback: FailCallback): SupportFragmentIntentHandler {
90+
super.failCallback(failCallback)
91+
return this
92+
}
93+
94+
override fun failIntent(failIntent: Intent): SupportFragmentIntentHandler {
95+
super.failIntent(failIntent)
96+
return this
97+
}
98+
99+
override fun failIntentHandler(failIntentHandler: ContextIntentHandler?): SupportFragmentIntentHandler {
100+
super.failIntentHandler(failIntentHandler)
101+
return this
102+
}
103+
}

core/src/main/java/com/omega_r/libs/omegaintentbuilder/interfaces/IntentHandler.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package com.omega_r.libs.omegaintentbuilder.interfaces
22

33
import android.app.Activity
4+
import android.app.Fragment
45
import android.content.ActivityNotFoundException
56
import com.omega_r.libs.omegaintentbuilder.handlers.ActivityIntentHandler
67
import com.omega_r.libs.omegaintentbuilder.handlers.ContextIntentHandler
8+
import com.omega_r.libs.omegaintentbuilder.handlers.FragmentIntentHandler
9+
import com.omega_r.libs.omegaintentbuilder.handlers.SupportFragmentIntentHandler
710

811
interface IntentHandler : IntentBuilder {
912

@@ -19,6 +22,18 @@ interface IntentHandler : IntentBuilder {
1922
*/
2023
fun createIntentHandler(activity: Activity): ActivityIntentHandler
2124

25+
/**
26+
* Returns FragmentIntentHandler (extends ContextIntentHandler) for control Intent.
27+
* Support startActivity, startActivityForResult, start Chooser.
28+
*/
29+
fun createIntentHandler(fragment: Fragment): FragmentIntentHandler
30+
31+
/**
32+
* Returns FragmentIntentHandler (extends ContextIntentHandler) for control Intent.
33+
* Support startActivity, startActivityForResult, start Chooser.
34+
*/
35+
fun createIntentHandler(fragment: android.support.v4.app.Fragment): SupportFragmentIntentHandler
36+
2237
/**
2338
* Same as {@link #startActivity(Intent, Bundle)} with no options specified.
2439
*

0 commit comments

Comments
 (0)