Home > Back-end >  Android Buttons disabled when back button pressed
Android Buttons disabled when back button pressed

Time:09-17

I am new to Android programming and I am confused by the activity life cycle. I have read a few articles on it but I still can't wrap my mind around how to make the back button work.

I have a Main Activity with buttons that go to new Activities and this works fine however when I use the device back button when it goes back to the Main Activity the buttons are disabled. I think this is because I need to re-initialize the Main Activity but I don't know how. I tries OnResume but it did not work. Here are my two activities:

package com.rootsofempathy.recoveryjunior

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.ImageButton

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val button_gr = findViewById<ImageButton>(R.id.gr_btn)
        button_gr.setOnClickListener {
            println("Button Clicked")
            val activity2Intent = Intent(applicationContext, getting_ready::class.java)
            startActivity(activity2Intent)
        }

        val buttonOne = findViewById<ImageButton>(R.id.theme1_btn)
        buttonOne.setOnClickListener {
            val activity2Intent = Intent(applicationContext, Theme_One::class.java)
            startActivity(activity2Intent)
        }

        val buttonTwo = findViewById<ImageButton>(R.id.theme2_btn)
        buttonTwo.setOnClickListener {
            val activity2Intent = Intent(applicationContext, Theme_Two::class.java)
            startActivity(activity2Intent)
        }

        val buttonThree = findViewById<ImageButton>(R.id.theme3_btn)
        buttonThree.setOnClickListener {
            val activity2Intent = Intent(applicationContext, Theme_Three::class.java)
            startActivity(activity2Intent)
        }

        val buttonFour = findViewById<ImageButton>(R.id.theme4_btn)
        buttonFour.setOnClickListener {
            val activity2Intent = Intent(applicationContext, Theme_Four::class.java)
            startActivity(activity2Intent)
        }

        val buttonFive = findViewById<ImageButton>(R.id.theme5_btn)
        buttonFive.setOnClickListener {
            val activity2Intent = Intent(applicationContext, Theme_Five::class.java)
            startActivity(activity2Intent)
        }

        val buttonSix = findViewById<ImageButton>(R.id.theme6_btn)
        buttonSix.setOnClickListener {
            val activity2Intent = Intent(applicationContext, Theme_Six::class.java)
            startActivity(activity2Intent)
        }

        val buttonSeven = findViewById<ImageButton>(R.id.theme7_btn)
        buttonSeven.setOnClickListener {
            val activity2Intent = Intent(applicationContext, Theme_Seven::class.java)
            startActivity(activity2Intent)
        }

        val buttonEight = findViewById<ImageButton>(R.id.theme8_btn)
        buttonEight.setOnClickListener {
            val activity2Intent = Intent(applicationContext, Theme_Eight::class.java)
            startActivity(activity2Intent)
        }

        val buttonNine = findViewById<ImageButton>(R.id.theme9_btn)
        buttonNine.setOnClickListener {
            val activity2Intent = Intent(applicationContext, Theme_Nine::class.java)
            startActivity(activity2Intent)
        }

        val buttonTen = findViewById<ImageButton>(R.id.theme10_btn)
        buttonTen.setOnClickListener {
            val activity2Intent = Intent(applicationContext, Theme_Ten::class.java)
            startActivity(activity2Intent)
        }
    }
}
class Theme_One : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_theme_one)

        val config = PdfActivityConfiguration.Builder(this@Theme_One).build()

        val assetFile = Uri.parse("file:///android_asset/theme_one.pdf")
        PdfActivity.showDocument(this@Theme_One, assetFile, null, config)

    }
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.rootsofempathy.recoveryjunior">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.RecoveryJunior">

        <activity
            android:name=".Theme_Ten"
            android:exported="true" />
        <activity
            android:name=".Theme_Nine"
            android:exported="true" />
        <activity
            android:name=".Theme_Eight"
            android:exported="true" />
        <activity
            android:name=".Theme_Seven"
            android:exported="true" />
        <activity
            android:name=".Theme_Six"
            android:exported="true" />
        <activity
            android:name=".Theme_Five"
            android:exported="true" />
        <activity
            android:name=".Theme_Four"
            android:exported="true" />
        <activity
            android:name=".Theme_Three"
            android:exported="true" />
        <activity
            android:name=".Theme_Two"
            android:exported="true" />
        <activity
            android:name=".Theme_One"
            android:exported="true" />
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.pspdfkit.ui.PdfActivity"
            android:theme="@style/RecoveryJunior.PSPDFKitTheme"
            android:windowSoftInputMode="adjustNothing" />
        <activity
            android:name=".getting_ready"
            android:theme="@style/Theme.AppCompat.Light.NoActionBar"
            android:windowSoftInputMode="adjustNothing" />

        <meta-data
            android:name="pspdfkit_license_key"
            android:value="" />
    </application>

</manifest>

If anyone can point me to a clear example on how to make the back button properly re-start the Main Activity buttons I would be grateful.

CodePudding user response:

You can solve this using two ways:

#1 Call finish() after starting PdfActivity

class Theme_One : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_theme_one)

        val config = PdfActivityConfiguration.Builder(this@Theme_One).build()

        val assetFile = Uri.parse("file:///android_asset/theme_one.pdf")
        PdfActivity.showDocument(this@Theme_One, assetFile, null, config)
        finish()
    }
}

#2 As per you current code you can directly start your PdfActivity and skip Theme_one activity on button click

buttonOne.setOnClickListener {
        
        val config = PdfActivityConfiguration.Builder(this).build()

        val assetFile = Uri.parse("file:///android_asset/theme_one.pdf")
        PdfActivity.showDocument(this@Theme_One, assetFile, null, config)

    }

CodePudding user response:

Your code looks fine. May the problem is PdfActivity.showDocument(//**//) remain opened.

Try to implement fragments for your use case.

  • Related