2

I am starting an activity with startActivityForResult() and sending an extra to it, then closing the second activity and going back to the previous one sending data back.

  • Only when launching the second activity (and not getting back to the first);

and

  • Only if the soft keyboard is open

and

  • Only on rooted devices or emulators,

This odd behavior happens.

I've tried the solutions posted here: Blinking screen on image transition between activities and here: Starting Activity on condition produces a flicker on screen with no success.

Here's the (trivial) code. Btw this (of course) happens either in Java or Kotlin (provided); and it also happens if I call startActivity() instead of startActivityForResult()

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        btn_main.setOnClickListener { launchSecondActivity() }
    }

    private fun launchSecondActivity() {
        Intent(this, SecondActivity::class.java).run {
            putExtra(EXTRA_MESSAGE, editText_main.text.toString())
            startActivityForResult(this, RETURN_MESSAGE_CODE) 
            Log.d("MainActivity", "Sending ${this.extras}")
            // clean the editText
            editText_main.setText("")
        }
    }
}
Dakatine
  • 2,734
  • 1
  • 19
  • 20

1 Answers1

1

When you launch the second activity keyboard is still visible and it is moving the layout up until it closes. Try following in your activity manifest

<activity android:windowSoftInputMode="adjustResize"> </activity>

adjustResize will not move toolbar up but resize the window height so hopefully screen won't flicker. If it does't help launch the activity with a delay to let keyboard close completely.

underoid
  • 429
  • 4
  • 11
  • It WORKED mate. Thank you! But... why that does NOT happen on MOST DEVICES (only rooted ones so far) And happens only when switching back from second to first activity? When I get back from second to first I am using a similar approach (keyboard on, tapping a button). – Dakatine Feb 07 '19 at 00:19
  • Actually I noticed that it does not do what I want. That just leaves the keyboard open – Dakatine Feb 07 '19 at 07:03
  • You're welcome! I think it is about activity creation time. First start and second start(go back) time of an activity may be different. Put your `startActivityForResult` at the end after `editText.setText()` and give it a try. Why only rooted... hard to guess – underoid Feb 07 '19 at 07:14
  • so use this – underoid Feb 07 '19 at 07:30
  • That worked! Again, I'm totally puzzled about why this only happens on some devices and emulators while on others do not. Is android programming always this random? Going to study deep what those options do. – Dakatine Feb 07 '19 at 07:57
  • sorry for the avalanche of comments but I'm realizing how heterogeneous this is. adjustResize was actually enough on the rooted emulator. On my rooted physical phone the keyboard did not disappear. With both options it works as I want on both devices. I'm totally puzzled. – Dakatine Feb 07 '19 at 08:01