Skip to content

Commit

Permalink
activity回调
Browse files Browse the repository at this point in the history
  • Loading branch information
br3ant committed Oct 22, 2020
1 parent df1a3c3 commit 4261abb
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 175 deletions.
3 changes: 2 additions & 1 deletion app/src/main/java/com/br3ant/tools/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.br3ant.tools

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

}
}
24 changes: 1 addition & 23 deletions utils/src/main/java/com/br3ant/base/BaseActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.WindowManager
import com.gyf.immersionbar.ImmersionBar
import me.yokeyword.fragmentation.SupportActivity

/**
Expand All @@ -27,7 +26,7 @@ abstract class BaseActivity(
or WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION
)
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
if (isFullScreen()) {
if (fullScreen) {
window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
}
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN)
Expand All @@ -54,29 +53,8 @@ abstract class BaseActivity(
Log.e("BaseActivity", "${javaClass.simpleName} --> initView Error!! e = ${e.message}")
e.printStackTrace()
}

//由于状态栏颜色修改为白色,所以需要把状态栏文字颜色改为深色,必须放在布局创建后
//全屏显示的界面不要用这个,会导致横屏的刘海颜色被设置为状态栏颜色
if (!isFullScreen() && isUseImmersionBar()) {
//1:MIUUI 2:Flyme 3:android6.0 otherwise 设置0.2f透明度

//不设置fitsSystemWindows会导致状态栏被挡住,设置后会导致全屏界面上面有白条
ImmersionBar.with(this)
.statusBarDarkFont(true)
.keyboardEnable(true)
.statusBarColor("#ffffff")
.fitsSystemWindows(true)
.init()
}
}

protected open fun isFullScreen(): Boolean {
return fullScreen
}

//是否使用ImmersionBar
protected open fun isUseImmersionBar(): Boolean = true

protected open fun getLayoutId(): Int = 0
protected open fun getThemeId(): Int = 0
protected abstract fun initView()
Expand Down
52 changes: 52 additions & 0 deletions utils/src/main/java/com/br3ant/utils/IntentExt.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.br3ant.utils

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.Parcelable
import java.io.Serializable

/**
* <pre>
* copyright: datedu
* @author : br3ant
* e-mail : xxx@xx
* time : 2020/10/22
* desc :
* version: 1.0
* </pre>
*/
inline fun <reified T : Context> Context.getIntent(vararg pairs: Pair<String, Any>): Intent =
Intent(this, T::class.java).apply {
pairs.forEach { pair ->
val name = pair.first
when (val value = pair.second) {
is Int -> putExtra(name, value)
is Byte -> putExtra(name, value)
is Char -> putExtra(name, value)
is Short -> putExtra(name, value)
is Boolean -> putExtra(name, value)
is Long -> putExtra(name, value)
is Float -> putExtra(name, value)
is Double -> putExtra(name, value)
is String -> putExtra(name, value)
is CharSequence -> putExtra(name, value)
is Parcelable -> putExtra(name, value)
is Array<*> -> putExtra(name, value)
is ArrayList<*> -> putExtra(name, value)
is Serializable -> putExtra(name, value)
is BooleanArray -> putExtra(name, value)
is ByteArray -> putExtra(name, value)
is ShortArray -> putExtra(name, value)
is CharArray -> putExtra(name, value)
is IntArray -> putExtra(name, value)
is LongArray -> putExtra(name, value)
is FloatArray -> putExtra(name, value)
is DoubleArray -> putExtra(name, value)
is Bundle -> putExtra(name, value)
is Intent -> putExtra(name, value)
else -> {
}
}
}
}
81 changes: 0 additions & 81 deletions utils/src/main/java/com/br3ant/utils/launcher/DLauncher.java

This file was deleted.

84 changes: 84 additions & 0 deletions utils/src/main/java/com/br3ant/utils/launcher/DLauncher.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.br3ant.utils.launcher

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException

/**
* <pre>
* copyright: datedu
* @author : houqiqi
* e-mail : xxx@xx
* time : 2019-11-21
* desc : Activity跳转封装类,把OnActivityResult方式改为Callback方式
* version: 1.0
</pre> *
*/
class DLauncher constructor(activity: FragmentActivity) {
private val mContext: Context
private val mRouterFragmentX: DRouterX?

constructor(fragment: Fragment) : this(fragment.requireActivity())

constructor(activity: Activity) : this(activity as FragmentActivity)


private fun getRouterFragmentX(activity: FragmentActivity): DRouterX? {

return findRouterFragmentX(activity) ?: DRouterX.newInstance().also {
val fragmentManager = activity.supportFragmentManager
fragmentManager
.beginTransaction()
.add(it, TAG)
.commitAllowingStateLoss()
fragmentManager.executePendingTransactions()
}
}

private fun findRouterFragmentX(activity: FragmentActivity): DRouterX? {
return activity.supportFragmentManager.findFragmentByTag(TAG) as DRouterX?
}

fun startActivityForResult(clazz: Class<*>?, callback: Callback?) {
val intent = Intent(mContext, clazz)
startActivityForResult(intent, callback)
}

fun startActivityForResult(intent: Intent?, callback: Callback?) {
if (mRouterFragmentX != null) {
mRouterFragmentX.startActivityForResult(intent, callback)
} else {
throw RuntimeException("please do init first!")
}
}

suspend fun launcher(intent: Intent?): Bundle? =
suspendCancellableCoroutine { continuation ->
try {
startActivityForResult(intent) { _, data ->
continuation.resume(data?.extras)
}
} catch (e: Exception) {
continuation.resumeWithException(e)
}
}

fun interface Callback {
fun onActivityResult(resultCode: Int, data: Intent?)
}

companion object {
private const val TAG = "DLauncher"
}

init {
mContext = activity
mRouterFragmentX = getRouterFragmentX(activity)
}
}
70 changes: 0 additions & 70 deletions utils/src/main/java/com/br3ant/utils/launcher/DRouterX.java

This file was deleted.

Loading

0 comments on commit 4261abb

Please sign in to comment.