我有2个 Activity ,其在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最初将以纵向启动约一秒钟,然后恢复为横向模式。如何解决这个问题?

BrowseBooks Activity :
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)


}

最佳答案

当我们在应用中将单个 Activity 作为肖像时,似乎是错误。
以下链接可能有帮助:
Android 8.1 screen orientation issue: flipping to landscape a portrait screen

09-11 19:17