我有2个活动,其默认方向在AndroidManifest.xml中指定
<activity android:name="com.package.name.Activities.BrowseBooksActivity"
android:screenOrientation="portrait"/>
<activity android:name="com.package.name.Activities.ReadBooksActivity"
android:screenOrientation="landscape"/>
用户单击BrowseBooksActivity中的项目(纵向)并启动ReadBooksActivity(横向)。但是当按下后退按钮时,BrowseBooksActivity最初以肖像形式开始大约一秒钟,然后恢复为横向模式。如何解决这个问题?
BrowseBooksActivity:
class BrowseBooksActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.browsebooksmain_layout)
fetchBooks ()
recyclerview_main.layoutManager = LinearLayoutManager(this)
}
fun fetchBooks () {
// some processing
runOnUiThread {
recyclerview_main.adapter = BrowseBooksAdapter(bookList)
}}
BrowseBooksAdapter:
class BrowseBooksAdapter (val bookFeed: List<ResourcesList>): RecyclerView.Adapter<CustomViewHolder>() {
override fun getItemCount(): Int {
return bookFeed.count()
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val cellForRow = layoutInflater.inflate(R.layout.browsebooks_layout, parent, false)
return CustomViewHolder(cellForRow)
}
override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {
holder.v.textview_booktitle?.text = bookFeed[position].name
Picasso.get().load(bookFeed[position].coverURI).into(holder.v.ImageView_bookcover)
holder.bookFeed = bookFeed[position]
}
}
class CustomViewHolder(val v: View, var bookFeed : ResourcesList? = null): RecyclerView.ViewHolder(v) {
init {
v.setOnClickListener {
val intent = Intent (v.context, ReadBooksActivity::class.java)
intent.putExtra("ID", bookFeed?.id)
v.context.startActivity(intent)
}
}
}
ReadBooksActivity:
class ReadBooksActivity : Activity() {
private var mCurlView: CurlView? = null
private var newbookList : ResourcesList? = null
public override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.readbooks_layout)
var index = 0
if (lastNonConfigurationInstance != null) {
index = lastNonConfigurationInstance as Int
}
mCurlView = findViewById(R.id.curl)
mCurlView!!.setSizeChangedObserver(SizeChangedObserver())
mCurlView!!.setCurrentIndex(index)
mCurlView!!.setBackgroundColor(-0xdfd7d0)
var bookId = intent.getIntExtra("ID", 0)
Thread (Runnable {
//some processing to load data to mBitmaps
mCurlView!!.setBitmapProvider(BitmapProvider(mBitmaps))
}).start()
mCurlView!!.setEnableTouchPressure(true)
mCurlView!!.set2PagesLandscape(true)
}
答案 0 :(得分:1)
当我们在app中将单个活动作为肖像时,似乎是错误。 以下链接可能有所帮助 Android 8.1 screen orientation issue: flipping to landscape a portrait screen
答案 1 :(得分:0)
对于仍然遇到此错误问题的人,以下是Sahil分享的链接中提供的解决方法。
在&#34;第二&#34;活动,添加以下内容:
onCreate(){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
}
onPause(){
if (android.os.Build.VERSION.SDK_INT >= 27) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
}
}
onResume(){
if (android.os.Build.VERSION.SDK_INT >= 27) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
}
}