Reputation: 4893
I have Activity A and Activity B in my Android App. In Activity A there is a button which leads to Activity B. In the Activity B I have a WebView with Internet Form. The user can enter some data into that form. If he goes back to Activity A and then again to Activity B he will loose that entered data. How can I prevent this from happeing?
Upvotes: 0
Views: 727
Reputation: 568
Firstly, you would need to add android:launchMode="singleInstance"
to both the activities in the manifest. This will ensure if an instance of an activity exists, then it will bring it forward and call its onNewIntent()
function rather than creating a new activity and calling its onCreate()
.
You could start Activity B from Activity A and make sure to NOT use finish()
.
For example in Activity A:
startActivity(Intent(this, ActivityB::class.java))
When the user wants to go back to Activity A from Activity B make sure to call
startActivity(Intent(this, ActivityA::class.java))
This would require you to override the hardware back button behaviour and maybe also the navigation back button if you are showing that as well. Kotlin example:
/* Top navigation button */
override fun onOptionsItemSelected(item: MenuItem): Boolean {
if (item.itemId == android.R.id.home) {
finish()
return true
}
return super.onOptionsItemSelected(item)
}
/* Hardware back button */
override fun onBackPressed() {
startActivity(Intent(this, MainActivity::class.java))
}
Upvotes: 1