Skip to content

Instantly share code, notes, and snippets.

View NezSpencer's full-sized avatar

Nnabueze Uhiara NezSpencer

  • Portugal
View GitHub Profile
@NezSpencer
NezSpencer / MainActivity.kt
Created March 9, 2019 15:13
Disable recyclerview nestedScrolling
//rvList is my recyclerview
binding.rvList.isNestedScrollingEnabled = false
@NezSpencer
NezSpencer / BackStackActivity.kt
Created October 13, 2018 13:21
pop off all fragments after the specified fragment, including the specified fragment
private fun popToFragmentBeforeKnownFragment(fragment: Fragment) {
if (!lifecycle.currentState.isAtLeast(Lifecycle.State.STARTED))
return
val isPopped = supportFragmentManager.popBackStackImmediate(fragment::class.simpleName,
FragmentManager.POP_BACK_STACK_INCLUSIVE)
if (!isPopped)
throw UnsupportedOperationException("Specified fragment does not exist in backstack")
}
@NezSpencer
NezSpencer / BackStackActivity.kt
Created October 7, 2018 11:02
move back x times in fragment backstack
private fun moveBackXTimes(numOfTimes : Int) {
if (!lifecycle.currentState.isAtLeast(Lifecycle.State.STARTED))
return
val backStackCount = supportFragmentManager.backStackEntryCount
if (numOfTimes >= backStackCount) {
throw UnsupportedOperationException("Number of times specified $numOfTimes is greater" +
" than the backstackEntryCount")
}
@NezSpencer
NezSpencer / BackStackActivity.kt
Created October 7, 2018 11:00
create synthetic / artificial backstack
private fun createArtificialBackStack() {
if (!lifecycle.currentState.isAtLeast(Lifecycle.State.STARTED))
return
supportFragmentManager.beginTransaction()
.replace(R.id.app_container, HomeFragment.newInstance(), HomeFragment::class.simpleName)
.addToBackStack(HomeFragment::class.simpleName)
.setReorderingAllowed(true)
.commit()
@NezSpencer
NezSpencer / BackStackActivity.kt
Created October 7, 2018 10:59
pop backstack to a known fragment
private fun popToKnownFragment(fragment : Fragment) {
if (!lifecycle.currentState.isAtLeast(Lifecycle.State.STARTED))
return
val isPopped = supportFragmentManager.popBackStackImmediate(fragment::class.simpleName, 0)
if (!isPopped) {
// code inside here is only executed because fragment transaction didnt happen because
// the fragment above does not exist in the backstack or it was added with a different tag.
// Since this fragment must be shown, add a new instance of it
supportFragmentManager.beginTransaction()
@NezSpencer
NezSpencer / BackStackActivity.kt
Created October 7, 2018 10:53
move back once in backstack
private fun moveBackOneTime() {
if (!lifecycle.currentState.isAtLeast(Lifecycle.State.STARTED))
return
supportFragmentManager.popBackStack()
}
@NezSpencer
NezSpencer / MainActivity.kt
Created October 1, 2018 09:36
Responsive naira amount textWatcher
package com.nezspencer.test
import android.os.Bundle
import android.support.annotation.NonNull
import android.support.v7.app.AppCompatActivity
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.widget.EditText
import java.text.NumberFormat
@NezSpencer
NezSpencer / MainActivity.kt
Created October 1, 2018 09:22
method to get new cursor position for amount textWatcher
private fun getNewCursorPosition(digitCountToRightOfCursor : Int, numberString : String) : Int{
var position = 0
var c = digitCountToRightOfCursor
for (i in numberString.reversed()) {
if (c == 0)
break
if (i.isDigit())
c --
position ++
@NezSpencer
NezSpencer / MainActivity.kt
Created September 30, 2018 08:54
illegal state exception prevention
override fun onSuccess(){
if(lifecycle.currentState.isAtLeast(Lifecycle.State.STARTED)) {
//safe to perform fragment transaction
}
}
@NezSpencer
NezSpencer / MainActivity.kt
Created September 30, 2018 07:48
query lifecycle state
@Override
fun onSuccess(){
if(lifecycle.currentState.isAtLeast(Lifecycle.State.STARTED)) {
//perform action here
}
}