Last active
September 18, 2020 07:49
-
-
Save Slowhand0309/7c558f32039091e167507b0ec4b98412 to your computer and use it in GitHub Desktop.
[Kotlin Extension Activity] #Kotlin #Android
This file contains hidden or 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
/** | |
* カスタムActivityの取得 | |
* 取得できない場合はnull | |
*/ | |
val Activity.rootApplication: RootApplication? | |
get() = (application as? RootApplication) | |
/** | |
* 現在フォーカスされているViewを元にKeyboardを非表示にする | |
*/ | |
fun Activity.hideKeyboardFromView() | |
= currentFocus?.apply { | |
val im = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager | |
im.hideSoftInputFromWindow(this.windowToken, InputMethodManager.HIDE_NOT_ALWAYS) | |
} | |
/** | |
* containerViewIdのFragmentを置き換える | |
*/ | |
fun <F> Activity.replaceFragment( | |
@IdRes containerViewId: Int, fragment: F) where F : Fragment { | |
supportFragmentManager.beginTransaction() | |
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN) | |
.replace(containerViewId, fragment) | |
.commit() | |
} | |
/** | |
* Activityスタート | |
* | |
* @param context {@link Context} | |
* @param cls Class | |
* @param request リクエストコード | |
* @param intentFlag Intentに設定するフラグ | |
*/ | |
fun Activity.startActivity( | |
context: Context, cls: Class<*>, request: Int = -1, intentFlag: Int = -1) { | |
val intent = Intent(context, cls) | |
if (intentFlag > 0) { | |
intent.flags = intentFlag | |
} | |
if (request > 0) { | |
startActivityForResult(intent, request) | |
} else { | |
startActivity(intent) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment