Reputation: 396
I have 2 activities with a default orientation specified in the AndroidManifest.xml
<activity android:name="com.package.name.Activities.BrowseBooksActivity"
android:screenOrientation="portrait"/>
<activity android:name="com.package.name.Activities.ReadBooksActivity"
android:screenOrientation="landscape"/>
The user clicks an item in BrowseBooksActivity (in portrait) and the ReadBooksActivity (in landscape) is launched. But when the back button is pressed, BrowseBooksActivity initially starts as portrait for about a second and then reverts to landscape mode. How to fix this?
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)
}
Upvotes: 1
Views: 740
Reputation: 396
For anyone still having problems with this bug, here is a workaround provided in the link shared by Sahil.
On the "second" activity, add the following:
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);
}
}
Upvotes: 0
Reputation: 972
Seems to be bug when we have single activity as portrait in app. Below link might help : Android 8.1 screen orientation issue: flipping to landscape a portrait screen
Upvotes: 1