Pass options bundle to startActivity introduce an unexpected result in destination Activity

221 Views Asked by At

The simple sample of this issue can be found here : https://github.com/alirezaeiii/SearchViewOption

I have following code in MainActivity:

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

        val button = findViewById<Button>(R.id.button)

        button.setOnClickListener {
            val options = ActivityOptions.makeSceneTransitionAnimation(
                this,
                button,"animation"
            ).toBundle()

            val intent = Intent(this, SearchActivity::class.java)
            startActivity(intent, options)
        }
    }
}

As you see I startActivty with options as bundle. In the SearchActivity I have following code :

class SearchActivity: AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_search)

        val button = findViewById<Button>(R.id.button)
        val search = findViewById<SearchView>(R.id.search)

        search.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(query: String): Boolean {
                return true
            }

            override fun onQueryTextChange(query: String): Boolean {
                if (query.isNotEmpty()) {
                    button.visibility = View.GONE
                } else {
                    button.visibility = View.VISIBLE
                }
                return true
            }
        })
    }
}

As you see I hide the button when there is a query. For the first time if you enter a query, turn off screen and turn it on (SearchActivity resumed), button appears again. if you search again and turn off screen and turn it on (SearchActivity resumed), button is gone as expected.

This issue is related to options bundle that I pass in startActivity. If I don't pass it and startActivity(intent) everything works as expected.

Why options bundle introduce this problem? How I can avoid it?

Addenda I reported this issue here : https://issuetracker.google.com/issues/251812970

0

There are 0 best solutions below