0

I have a service in my code that is meant to display a different brain teaser question each time the service is called on. I can get the question to change on my service, but when I have the service shut down (to go to a different service/activity) and then call on it later, it reverts back to the initial value of the textveiw (i.e., whatever the first brainteaser was) instead of changing its text. I use a map to store my brainteaser questions and their answer and I use two global variables that update every time the service is finished to access the keys and subsequent brainteaser questions in my map (this is to change the brainteaser and its answer). The variables update fine but still the text and answer displayed don't change. I've looked up about loopers, handlers and that but I can't find the answer to what I'm looking for and I don't really understand them. Sorry if this is a simple answer but I honestly have no idea what to do as this is my first coding project. Below is the code for my service. Let me know if I've left anything out or need to give more information/explain better! Thanks in advance.

class BrainScreen : Service(){

private lateinit var brainFloatView: ViewGroup
private lateinit var brainfloatWindowLayoutParems: WindowManager.LayoutParams
private var LAYOUT_TYPE: Int? = null
private lateinit var windowManger: WindowManager
private lateinit var textInput: EditText
private lateinit var checkSMALLbutton: Button
private lateinit var textDisplay: TextView
private lateinit var Qanswer: String

var brainQ = 1
var brainA = brainQ*11

override fun onBind(intent: Intent?): IBinder? {
    return null
}

override fun onCreate() {
    super.onCreate()

    val metrics = applicationContext.resources.displayMetrics
    val width = metrics.widthPixels
    val height = metrics.heightPixels

    val brainteaserQS: Map<Int, String> = mapOf(
        1 to "I have keys but no locks. I have space but no room. You can enter, but you can't go outside. What am I?",
        11 to "Keyboard",
        2 to "If you drop me, I’m sure to crack, but smile at me and I’ll smile back. What am I?",
        22 to "Mirror",
        3 to "What has hands and a face, but can’t hold anything or smile?",
        33 to "Clock",
        4 to "The more you take, the more you leave behind. What are they?",
        44 to "Footsteps"
    )

    windowManger = getSystemService(WINDOW_SERVICE) as WindowManager

    val inflater = baseContext.getSystemService(LAYOUT_INFLATER_SERVICE) as LayoutInflater

    brainFloatView = inflater.inflate(R.layout.activity_brain_screen, null) as ViewGroup

    checkSMALLbutton = brainFloatView.findViewById(R.id.checkSMALLbutton)
    textInput = brainFloatView.findViewById(R.id.answerInput)
    //BELOW IS WHERE I SET MY TEXTVIEW VALUES (AND THE BRAINTEASER ANSWER)
    
    textDisplay = brainFloatView.findViewById(R.id.brainteaser)
    textDisplay.text = brainteaserQS[brainQ]
    Qanswer = brainteaserQS[brainA].toString()

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        LAYOUT_TYPE = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
    } else LAYOUT_TYPE = WindowManager.LayoutParams.TYPE_TOAST 

    brainfloatWindowLayoutParems = WindowManager.LayoutParams(
        (width * 0.9).toInt(),
        (height * 0.46).toInt(),
        LAYOUT_TYPE!!,
        WindowManager.LayoutParams.FLAG_LOCAL_FOCUS_MODE,
        PixelFormat.TRANSLUCENT
    )

    brainfloatWindowLayoutParems.gravity = Gravity.TOP
    brainfloatWindowLayoutParems.x = 0
    brainfloatWindowLayoutParems.y = 0

    windowManger.addView(brainFloatView, brainfloatWindowLayoutParems)

    checkSMALLbutton.setOnClickListener() {

        stopSelf()
        windowManger.removeView(brainFloatView)

        if (Qanswer == textInput.text.toString()) {
            //IF THE PERSON ANSWERS THE BRAINTEASER RIGHT THE TEXTVIEW SHOULD CHANGE AS THE SERVICE SHUTS DOWN SO WHEN THE SERVICE IS CALLED AGAIN THE BRAINTEASER WILL BE DIFFERENT
            brainQ += 1
            brainA = brainQ*11
            
            textDisplay.text = brainteaserQS[brainQ]
            Qanswer = brainteaserQS[brainA].toString()
            startService(Intent(this@BrainScreen,CorrectScreen::class.java))

        } else {
            startService(Intent(this@BrainScreen,TryAgainScreen::class.java))
            }
        }

    }

}

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.