我是Android編程的新手,我對活動生命周期感到困惑。 我已經閱讀了一些相關的文章,但我仍然無法理解如何使后退按鈕發揮作用。
我有一個帶有按鈕的主活動,這些按鈕可以轉到新的活動中,并且作業正常,但是當我使用設備的回傳按鈕時,當它回傳到主活動時,按鈕被禁用。 我認為這是因為我需要重新初始化主活動,但我不知道如何做。 我嘗試了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(saidInstanceState: Bundle?) {
super.onCreate(s 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(sedInstanceState: Bundle? ){
super.onCreate(s 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"/span>
package="com.rootsofempathy.recoveryjunior"/span>>
<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"/span>>
<activity
android:name=".Theme_Ten"。
android:exported="true" />
<activity
android:name=".Theme_Nine"/span>
android:exported="true" />
<activity
android:name=".Theme_Eight"/span>
android:exported="true" />
<activity
android:name=".Theme_Seven"/span>
android:exported="true" />
<activity
android:name=".Theme_Six"/span>
android:exported="true" />
<activity
android:name=".Theme_Five"/span>
android:exported="true" />
<activity
android:name=".Theme_Four"/span>
android:exported="true" />
<activity
android:name=".Theme_Three"/span>
android:exported="true" />
<activity
android:name=".Theme_Two"/span>
android:exported="true" />
<activity
android:name=".Theme_One"/span>
android:exported="true" />
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>/span>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"/span> />
</intent-filter>
</activity>/span>
<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>
如果有人能給我指出一個明確的例子,說明如何使后退按鈕正確地重新啟動主要活動按鈕,我將不勝感激。
uj5u.com熱心網友回復:
你可以用兩種方法來解決這個問題:
#1 在啟動PdfActivity后呼叫finish()
class Theme_One : AppCompatActivity(){
override fun onCreate(sedInstanceState: Bundle? ){
super.onCreate(s 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)
完成()
}
}
#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)
}
uj5u.com熱心網友回復:
你的代碼看起來很好。
可能問題在于PdfActivity.showDocument(//**//)仍然被打開。
嘗試為你的用例實作片段。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/311865.html
標籤:
