我是 Android 编程的新手,我对 Activity 生命周期感到困惑。我已经阅读了一些关于它的文章,但我仍然无法思考如何使后退按钮起作用。
我有一个带有转到新活动的按钮的主活动,这工作正常,但是当我使用设备返回按钮返回主活动时,按钮被禁用。我认为这是因为我需要重新初始化 Main Activity 但我不知道如何。我尝试了 OnResume 但它没有用。这是我的两个活动:
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>
如果有人能指出一个关于如何使后退按钮正确重新启动主活动按钮的清晰示例,我将不胜感激。
您可以使用两种方法解决此问题:
#1finish()
启动后调用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 根据您当前的代码,您可以通过单击按钮直接启动PdfActivity
和跳过Theme_one
活动
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)
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句