我有两个活动。活动 A包含包含图像的卡片视图元素的回收器视图。在选择活动 A中的元素时。图像的完整横向版本显示在活动 B中。
活动 B的布局文件如下所示:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
活动 B 的代码库如下所示:
class PosterViewActivity : AppCompatActivity()
{
private lateinit var imageView: AppCompatImageView
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_poster_view)
init()
}
private fun init()
{
findViewsByID()
getBundle()
toggleHide()
}
private fun getBundle()
{
if (intent != null)
{
var imageUrl = intent.getStringExtra(ARG_IMAGE_URL)
if (imageUrl != null)
{
Glide.with(this).load(imageUrl).centerInside().into(imageView)
}
}
}
private fun findViewsByID()
{
imageView = findViewById(R.id.imageView)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean
{
if (item.itemId == android.R.id.home)
{
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
}
return super.onOptionsItemSelected(item)
}
override fun onBackPressed()
{
super.onBackPressed()
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
}
override fun onWindowFocusChanged(hasFocus: Boolean)
{
super.onWindowFocusChanged(hasFocus)
}
private fun toggleHide()
{
val uiOptions = window.decorView.systemUiVisibility
var newUiOptions = uiOptions
val isImmersiveModeEnabled = uiOptions or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY == uiOptions
if (isImmersiveModeEnabled)
{
Log.i("PosterViewActivity", "Turning immersive mode mode off. ")
}
else
{
Log.i("PosterViewActivity", "Turning immersive mode mode on.")
}
if (Build.VERSION.SDK_INT >= 14)
{
newUiOptions = newUiOptions xor View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
}
// Status bar hiding: Backwards compatible to Jellybean
if (Build.VERSION.SDK_INT >= 16)
{
newUiOptions = newUiOptions xor View.SYSTEM_UI_FLAG_FULLSCREEN
}
if (Build.VERSION.SDK_INT >= 18)
{
newUiOptions = newUiOptions xor View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
}
window.decorView.systemUiVisibility = newUiOptions
}
}
但是,当我按下设备上的后退按钮而不是返回活动 A时。状态和导航按钮将显示和隐藏,只需按 4 次后退按钮即可返回活动 A。我已经检查了后退堆栈,堆栈中有两个活动,即A 和 B
你应该在你的PosterViewActivity中做一些类似的事情:
//link instance of menu_main
val mainToolbar = findViewById<androidx.appcompat.widget.Toolbar>(R.id.toolbar)
setSupportActionBar(mainToolbar)
//Enable Up Button to return you to home page
val actionBar = supportActionBar
actionBar?.title = null
actionBar?.setDisplayHomeAsUpEnabled(true)