Skip to content

Instantly share code, notes, and snippets.

@monmonja
Created April 26, 2016 15:29
Show Gist options
  • Save monmonja/68238ed401358795df76f96ef6821166 to your computer and use it in GitHub Desktop.
Save monmonja/68238ed401358795df76f96ef6821166 to your computer and use it in GitHub Desktop.
Example of view related kotlin extension in android
package com.tutorialforandroid.testing
import android.content.Intent
import android.net.Uri
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentActivity
import android.support.v4.app.FragmentManager
import android.support.v7.app.AppCompatActivity
import com.tutorialforandroid.testing.ui.MainActivityUI
/**
* Created by almondjoseph on 23/4/16.
*/
fun goToFragment(supportFragmentManager: FragmentManager, fragment: Fragment) {
val fragmentTag = fragment.javaClass.simpleName
val fragmentPopped = supportFragmentManager.popBackStackImmediate(fragmentTag, 0)
if (!fragmentPopped && supportFragmentManager.findFragmentByTag(fragmentTag) == null) {
supportFragmentManager
.beginTransaction()
.replace(MainActivityUI.FRAME_LAYOUT_ID, fragment, fragmentTag)
.addToBackStack(fragment.javaClass.simpleName)
.commit()
}
}
fun AppCompatActivity.goToFragment(fragment: Fragment) {
goToFragment(supportFragmentManager, fragment)
}
fun FragmentActivity.goToFragment(fragment: Fragment) {
goToFragment(supportFragmentManager, fragment)
}
fun Fragment.gotoUrl(url: String) {
val intent: Intent = Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment