From 4261abba9158d18a42595b2d776c5fd3974bd162 Mon Sep 17 00:00:00 2001 From: br3ant <1106617567@qq.com> Date: Thu, 22 Oct 2020 20:20:12 +0800 Subject: [PATCH] =?UTF-8?q?activity=E5=9B=9E=E8=B0=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/br3ant/tools/MainActivity.kt | 3 +- .../main/java/com/br3ant/base/BaseActivity.kt | 24 +----- .../main/java/com/br3ant/utils/IntentExt.kt | 52 ++++++++++++ .../com/br3ant/utils/launcher/DLauncher.java | 81 ------------------ .../com/br3ant/utils/launcher/DLauncher.kt | 84 +++++++++++++++++++ .../com/br3ant/utils/launcher/DRouterX.java | 70 ---------------- .../com/br3ant/utils/launcher/DRouterX.kt | 60 +++++++++++++ 7 files changed, 199 insertions(+), 175 deletions(-) create mode 100644 utils/src/main/java/com/br3ant/utils/IntentExt.kt delete mode 100644 utils/src/main/java/com/br3ant/utils/launcher/DLauncher.java create mode 100644 utils/src/main/java/com/br3ant/utils/launcher/DLauncher.kt delete mode 100644 utils/src/main/java/com/br3ant/utils/launcher/DRouterX.java create mode 100644 utils/src/main/java/com/br3ant/utils/launcher/DRouterX.kt diff --git a/app/src/main/java/com/br3ant/tools/MainActivity.kt b/app/src/main/java/com/br3ant/tools/MainActivity.kt index 639d09f..cb9f117 100644 --- a/app/src/main/java/com/br3ant/tools/MainActivity.kt +++ b/app/src/main/java/com/br3ant/tools/MainActivity.kt @@ -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) + } } \ No newline at end of file diff --git a/utils/src/main/java/com/br3ant/base/BaseActivity.kt b/utils/src/main/java/com/br3ant/base/BaseActivity.kt index 3fb05cf..dccc123 100644 --- a/utils/src/main/java/com/br3ant/base/BaseActivity.kt +++ b/utils/src/main/java/com/br3ant/base/BaseActivity.kt @@ -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 /** @@ -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) @@ -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() diff --git a/utils/src/main/java/com/br3ant/utils/IntentExt.kt b/utils/src/main/java/com/br3ant/utils/IntentExt.kt new file mode 100644 index 0000000..d5cd85b --- /dev/null +++ b/utils/src/main/java/com/br3ant/utils/IntentExt.kt @@ -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 + +/** + *
+ * copyright: datedu + * @author : br3ant + * e-mail : xxx@xx + * time : 2020/10/22 + * desc : + * version: 1.0 + *+ */ +inline fun
- * copyright: datedu - * @author : houqiqi - * e-mail : xxx@xx - * time : 2019-11-21 - * desc : Activity跳转封装类,把OnActivityResult方式改为Callback方式 - * version: 1.0 - *- */ -public class DLauncher { - private static final String TAG = "DLauncher"; - private Context mContext; - - private DRouterX mRouterFragmentX; - - public static DLauncher init(Fragment fragment) { - return init(fragment.requireActivity()); - } - - public static DLauncher init(Activity activity) { - return init((FragmentActivity) activity); - } - - public static DLauncher init(FragmentActivity activity) { - return new DLauncher(activity); - } - - - private DLauncher(FragmentActivity activity) { - mContext = activity; - mRouterFragmentX = getRouterFragmentX(activity); - } - - private DRouterX getRouterFragmentX(FragmentActivity activity) { - DRouterX routerFragment = findRouterFragmentX(activity); - if (routerFragment == null) { - routerFragment = DRouterX.newInstance(); - FragmentManager fragmentManager = activity.getSupportFragmentManager(); - fragmentManager - .beginTransaction() - .add(routerFragment, TAG) - .commitAllowingStateLoss(); - fragmentManager.executePendingTransactions(); - } - return routerFragment; - } - - private DRouterX findRouterFragmentX(FragmentActivity activity) { - return (DRouterX) activity.getSupportFragmentManager().findFragmentByTag(TAG); - } - - public void startActivityForResult(Class> clazz, Callback callback) { - Intent intent = new Intent(mContext, clazz); - startActivityForResult(intent, callback); - } - - public void startActivityForResult(Intent intent, Callback callback) { - if (mRouterFragmentX != null) { - mRouterFragmentX.startActivityForResult(intent, callback); - } else { - throw new RuntimeException("please do init first!"); - } - } - - public interface Callback { - void onActivityResult(int resultCode, @Nullable Intent data); - } -} diff --git a/utils/src/main/java/com/br3ant/utils/launcher/DLauncher.kt b/utils/src/main/java/com/br3ant/utils/launcher/DLauncher.kt new file mode 100644 index 0000000..b5c3e2e --- /dev/null +++ b/utils/src/main/java/com/br3ant/utils/launcher/DLauncher.kt @@ -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 + +/** + *
+ * copyright: datedu + * @author : houqiqi + * e-mail : xxx@xx + * time : 2019-11-21 + * desc : Activity跳转封装类,把OnActivityResult方式改为Callback方式 + * version: 1.0 +* + */ +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) + } +} \ No newline at end of file diff --git a/utils/src/main/java/com/br3ant/utils/launcher/DRouterX.java b/utils/src/main/java/com/br3ant/utils/launcher/DRouterX.java deleted file mode 100644 index 827d975..0000000 --- a/utils/src/main/java/com/br3ant/utils/launcher/DRouterX.java +++ /dev/null @@ -1,70 +0,0 @@ -package com.br3ant.utils.launcher; - -import android.content.Intent; -import android.os.Bundle; -import android.util.SparseArray; - -import androidx.annotation.Nullable; -import androidx.fragment.app.Fragment; - -import java.util.Random; - -/** - *
- * copyright: datedu - * @author : houqiqi - * e-mail : xxx@xx - * time : 2019-11-21 - * desc : 把OnActivityResult方式转换为Callback方式的空Fragment(X兼容包) - * version: 1.0 - *- */ -public class DRouterX extends Fragment { - private SparseArray
+ * copyright: datedu + * @author : houqiqi + * e-mail : xxx@xx + * time : 2019-11-21 + * desc : 把OnActivityResult方式转换为Callback方式的空Fragment(X兼容包) + * version: 1.0 +* + */ +class DRouterX : Fragment() { + private val mCallbacks = SparseArray