Can I change what that button does. I tried just using it but when the user changes something and goes back, the changes aren't applied
Asked
Active
Viewed 144 times
0
-
Do you have a programming language? Tag it appropriately. – LarsTech Mar 14 '20 at 21:34
-
It's in java, for android studio – Abdallah Alqashqish Mar 14 '20 at 21:42
-
1Use the edit link under your question to fill out the tags appropriately. – LarsTech Mar 14 '20 at 21:44
-
You can check correct answer – MMG May 14 '20 at 09:34
3 Answers
0
It's very easy, just override onBackPress
of your activity.
Check documentation for more info - https://developer.android.com/reference/android/app/Activity#onBackPressed()

Alex Grebennikov
- 170
- 5
- 11
-
-
I've tried overriding the onBackPress. It gives me an error saying "Annotations are not allowed here" – Abdallah Alqashqish Mar 14 '20 at 22:40
-
1st comment - yes. 2nd comment - can you provide your code? seems like it is not the method override problem – Alex Grebennikov Mar 16 '20 at 05:25
-
1Oh, I figured it out. The problem was that I was trying to override it in the onCreate() method. Now it works. Thank you. – Abdallah Alqashqish Mar 18 '20 at 16:06
0
Did you search for it?
Android - How To Override the "Back" button so it doesn't Finish() my Activity?
you can override onBackPressed()
method in your activity

Erfan
- 83
- 4
- 9
0
Try this simple code:
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
override fun onBackPressed() {
// super.onBackPressed()
Toast.makeText(this,"Back Pressed",Toast.LENGTH_LONG).show()
}
}

MMG
- 3,226
- 5
- 16
- 43