Android on back click

Function called during back button click on Android

Which is the function called when you click the back button on android. My requirement is when an application is running and user clicks on the back button, the status of the application should be stored into the database and the user should be able to see the status whenever he returns back to the application.

Browsing through internet, I understood we can handle the control using onKeyDown() function. However even if I use it in my code, the function is not called when I click on back button. Below is the function:

Please suggest, if anybody has faced the same/similar scenario.

5 Answers 5

the activity function called when you press the back button is onBackPressed.

void onBackPressed() Called when the activity has detected the user’s press of the back key.

upvote if you came here for this and not specifically for saving state 😛

Use the Activity lifecycle methods to save and restore state. You could save your state during onPause and restore in onResume. In your activity:

You could also do this in onStart/onStop. Check out the activity lifecycle here: http://developer.android.com/reference/android/app/Activity.html

You don’t have to override the back button behavior to save the status of your activity.

Just override onPause() and save any persistent state you want.

Save changes to SharedPreferences and commit changes back to your SQLite databases in onPause . Load them in onResume .

Источник

Android — on clicking back

Hello I want to make an Activity, that on clicking back it closes, in stead of going to the previous activity. Please tell me how to do it. Thank you in advance

3 Answers 3

Try putting this in your activity class:

You can receive press by overriding ‘onBackPressed’, for example:

Nevertheless, this is bad practice. You shouldn’t exit app when user press back button. If user would wanted to close app. He would press home button. Maybe you should read this.. Is quitting an application frowned upon?

I strongly advise against approaching your problem as «I want to close my application when I press back», as this tends to migrate towards the evil Android anti-pattern of killing the process (as suggested in other answers).

Instead, I would focus more on the fact that you do not want previous Activities in your application to be saved in your application’s back stack. A simple way to do this is whenever you call startActivity(. ) you call finish() immediately afterwards, removing the old Activity from your back stack. When you press Back, the «previous» Activity is now whatever started your application in the first place (likely the user’s application launcher), and will be shown.

Читайте также:  Что должен знать джуниор андроид разработчик

Alternatively, if you also want the application to close when the user navigates to a different application (such as using Recent Tasks, opening a notification, or receiving a phone call), you can add the noHistory flag to your activities in your manifest, as follows:

The noHistory flag ensures that when the user navigates away from the Activity , it is automatically finished, via finish() .

Источник

How to implement the Android ActionBar back button?

I have an activity with a listview. When the user click the item, the item «viewer» opens:

This works fine, but on the actionbar the back arrow next to the app icon doesn’t get activated. Am I missing something?

14 Answers 14

Selvin already posted the right answer. Here, the solution in pretty code:

The function NavUtils.navigateUpFromSameTask(this) requires you to define the parent activity in the AndroidManifest.xml file

See here for further reading.

Make sure your the ActionBar Home Button is enabled in the Activity:

Android, API 5+:

ActionBarSherlock and App-Compat, API 7+:

Android, API 11+:

Example MainActivity that extends ActionBarActivity :

This way all the activities you want can have the backpress.

Android, API 16+:

Example MainActivity that extends ActionBarActivity :

To enable the ActionBar back button you obviously need an ActionBar in your Activity. This is set by the theme you are using. You can set the theme for your Activity in the AndroidManfiest.xml . If you are using e.g the @android:style/Theme.NoTitleBar theme, you don’t have an ActionBar. In this case the call to getActionBar() will return null. So make sure you have an ActionBar first.

The next step is to set the android:parentActivityName to the activity you want to navigate if you press the back button. This should be done in the AndroidManifest.xml too.

Now you can enable the back button in the onCreate method of your «child» activity.

Now you should implement the logic for the back button. You simply override the onOptionsItemSelected method in your «child» activity and check for the id of the back button which is android.R.id.home .

Now you can fire the method NavUtils.navigateUpFromSameTask(this); BUT if you don’t have specified the android:parentActivityName in you AndroidManifest.xml this will crash your app.

Sometimes this is what you want because it is reminding you that you forgot «something». So if you want to prevent this, you can check if your activity has a parent using the getParentActivityIntent() method. If this returns null, you don’t have specified the parent.

In this case you can fire the onBackPressed() method that does basically the same as if the user would press the back button on the device. A good implementation that never crashes your app would be:

Please notice that the animation that the user sees is different between NavUtils.navigateUpFromSameTask(this); and onBackPressed() .

It is up to you which road you take, but I found the solution helpful, especially if you use a base class for all of your activities.

Читайте также:  Грибной навигатор для андроид

Источник

Android: Go back to previous activity

I want to do something simple on android app. How is it possible to go back to a previous activity.

What code do I need to go back to previous activity

23 Answers 23

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.

Keep track of the activity stack. Whenever you start a new activity with an intent you can specify an intent flag like FLAG_ACTIVITY_REORDER_TO_FRONT or FLAG_ACTIVITY_PREVIOUS_IS_TOP . You can use this to shuffle between the activities in your application. Haven’t used them much though. Have a look at the flags here: http://developer.android.com/reference/android/content/Intent.html

As mentioned in the comments, if the activity is opened with startActivity() then one can close it with finish() . If you wish to use the Up button you can catch that in onOptionsSelected(MenuItem item) method with checking the item ID against android.R.id.home unlike R.id.home as mentioned in the comments.

Try Activity#finish() . This is more or less what the back button does by default.

Just write on click finish(). It will take you to the previous Activity.

This will get you to a previous activity keeping its stack and clearing all activities after it from the stack.

For example, if stack was A->B->C->D and you start B with this flag, stack will be A->B

Are you wanting to take control of the back button behavior? You can override the back button (to go to a specific activity) via one of two methods.

For Android 1.6 and below:

Or if you are only supporting Android 2.0 or greater:

Just call these method to finish current activity or to go back by onBackPressed

Add this in your onCLick() method, it will go back to your previous activity

or You can use this. It worked perfectly for me

if you want to go to just want to go to previous activity use

if you want to go to second activity or below that use following:

If you have setup correctly the AndroidManifest.xml file with activity parent, you can use :

Where this is your child activity.

Try this is act as you have to press the back button

Got the same problem and

worked fine for me, both worked same, but no luck with return

You can explicitly call onBackPressed is the easiest way
Refer Go back to previous activity for details

Start the second activity using intent (either use startActivity or startActivityForResult according to your requirements). Now when user press back button, the current activity on top will be closed and the previous will be shown.

Now Lets say you have two activities, one for selecting some settings for the user, like language, country etc, and after selecting it, the user clicks on Next button to go to the login form (for example) . Now if the login is unsuccessful, then the user will be on the login activity, what if login is successful ?

Читайте также:  Viber для андроид полная версия

If login is successful, then you have to start another activity. It means a third activity will be started, and still there are two activities running. In this case, it will be good to use startActivityForResult . When login is successful, send OK data back to first activity and close login activity. Now when the data is received, then start the third activity and close the first activity by using finish.

Источник

How do I kill an Activity when the Back button is pressed?

I got an Activity that when it starts, it loads an image from the internet. In an effort to save memory, when the Activity is left by the back button being pressed, I want the activity to dump all data, that is get rid of all the strings and images that are in it. I figured the best way to do this was to just kill the activity.

Well, I can’t seem to figure out the callback for when the Back button is pressed. So, I have been trying to use the onPause() and the onStop() callbacks for the task but both ways force close my app. Here is the code:

I’ve tried multiple variations of this, but none of them seemed to work. Any ideas?

5 Answers 5

Simple Override onBackPressed Method:

add this to your activity

My app closed with above code.

First of all, finish() doesn’t destroy your process and free up the memory. It just removes the activity from the activity stack. You’d need to kill the process, which is answered in a bunch of questions (since this is being asked several times).

But the proper answer is — Don’t do it. the Android OS will automatically free up memory when it needs memory. By not freeing up memory, your app will start up faster if the user gets back to it.

Please see here for a great write-up on the topic.

Well, if you study the structure of how the application life-cycle works,here , then you’ll come to know that onPause() is called when another activity gains focus, and onStop() is called when the activity is no longer visible.

From what I have learned yet, you can call finish() only from the activity which is active and/or has the focus. If you’re calling finish() from the onPause() method that means you’re calling it when the activity is no longer active. thus an exception is thrown.

When you’re calling finish() from onStop() then the activity is being sent to background, thus will no longer be visible, then this exception.

When you press the back button, onStop() is called.

Most probably, Android will automatically do for you what you are currently wanting to do.

Источник

Оцените статью