I need a back button with same functionality as default back button of device but inside of app.
Asked
Active
Viewed 745 times
-3
-
1It's really a poorly-worded question, but if all you need to do is go "back" from an activity, add a button and invoke `finish()` when it's clicked. Hard to believe that the documentation didn't tell you this. – 323go Apr 29 '15 at 19:34
-
1You can emulate a back press by calling [onBackPressed()](http://developer.android.com/reference/android/app/Activity.html#onBackPressed()) – PPartisan Apr 29 '15 at 19:37
-
Another common approach is to take advantage of the `ActionBar` and their navigation controls. This is another accepted approach for navigation internally outside the back button built into the system. – Jay Snayder Apr 29 '15 at 19:58
1 Answers
1
Android activities are stored in the activity stack. You can go back by just finishing the current activity:
add an onClick
listener to your button in the activity xml like this:
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/self_destruct"
android:onClick="selfDestruct" />
then create a method in your activity with the same name you specified in the xml
public void selfDestruct(View view) {
finish()
}
You can read more in the docs.

Eddie Martinez
- 13,582
- 13
- 81
- 106
-
eduardo thank you for your answer and when i add that code it tells me that cannot resolve setOnClickListener and v – BegCoder Apr 29 '15 at 20:32
-
did you copy and paste ? you need to add your button id instead of R.id.buttonId – Eddie Martinez Apr 29 '15 at 20:34
-
thank you it worked with my app.. but i have another question and if you could answer it i wouldnt post it.. i buy app on one site app works fine but now i wanted to add back button code and when i open java file it starts showing errors cannot resolve symbol and shows different symbols before i opened java file everything was fine.. i haven't changed anything. what could be problem? – BegCoder Apr 29 '15 at 21:17
-
-
I dont understand the question, ask a new one and add a screen shot or something, and give me a link. Make it as simple, and straight forward as possible so you could get up voted and get points. – Eddie Martinez Apr 29 '15 at 22:10
-
i have posted question. but i was unable to add screenshot don't have enough points – BegCoder Apr 29 '15 at 22:43
-
http://stackoverflow.com/questions/29955787/android-studio-cannot-resolve-symbols – BegCoder Apr 30 '15 at 18:25