Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set onBackButton listener to an activity

I have a Class A which runs activity via startActivityForResult by passing Intent to it. In other class, lets say B I get this Intent and recreate activity by it. How can I listen events for that activity, e.g. activity which was started for result is running and user pressed "back" button so I want to do some action. How can I do this? Thank you on advance.

Activity in which i recreate instance of object is not derived from Activity class. It is just ACTIVITY. So I have only object. is there any way to do such stuff with instance of class but not a class?

like image 307
unresolved_external Avatar asked Apr 03 '12 14:04

unresolved_external


People also ask

How do I go back to previous activity on Android?

Android activities are stored in the activity stack. Going back to a previous activity could mean two things. You opened the new activity from another activity with startActivityForResult. In that case you can just call the finishActivity() function from your code and it'll take you back to the previous activity.

How do you implement back pressed activity?

In order to check when the 'BACK' button is pressed, use onBackPressed() method from the Android library. Next, perform a check to see if the 'BACK' button is pressed again within 2 seconds and will close the app if it is so. Otherwise, don't exit.

Do all Android devices have a Back button?

All Android devices provide a Back button for this type of navigation, so you should not add a Back button to your app's UI. Depending on the user's Android device, this button might be a physical button or a software button.


1 Answers

You should override the method : onBackPressed() of the Activity class.

like image 136
Ovidiu Latcu Avatar answered Sep 25 '22 17:09

Ovidiu Latcu