Android :: Using Back Button
Jul 15, 2010I am a new to android development I want to override the default back button operation in android,please let me know how i can do this in code.
I am a new to android development I want to override the default back button operation in android,please let me know how i can do this in code.
This is my first android device and i'm loving it... but one thing that irks me is the use, or lack thereof, of the back button.
As I am navigating menus and options in the Settings, I cannot simply go back one menu using the back key. For example, if I go to Settings->Battery Manager->Battery Profile Options and want to go back to the Settings screen, I cannot.
Pushing the back button does nothing and I can only go back to the Settings menu by going Home, then re-entering.
Another quirk regarding the back button is when reading emails. Using a regular email account such as Hotmail, I cannot simply read an email and go back to the inbox using the button.
It makes navigating the phone very tedious.
I have 3 activities in my app: Activity1 -> Activity2 -> Activity3. Inside Activity3, if the user presses Back, I would like to return to Activity2. In Activity3's onPause event, I added a finish() statement. That's probably not even necessary, but I wanted to make sure this Activity gets cleaned up. This works fine.
However, while in Activity3, if the user presses Home or starts a new app (through notification bar or some other means), I want both Activity3 and Activity2 to finish. If the user returns to this app, he should resume with Activity1.
I have figured out how to do one or the other, but I can't figure out how to handle both cases, if it's even possible. Can I trap the "Back" button in Activity3 and send a message back to Activity2 telling it not to finish()? It seems like the Activities follow the same lifecycle flow (Pause, Stop) regardless of what you do to send them to the background. Just to answer the question of why I want this behavior, imagine that Activity1 is a login screen, Activity2 is a selection screen, and Activity3 is a content screen. If I press Back from the content page, I want to be able to make a new selection. If I exit via any other means (Home, notification bar), I want the user to be "logged out".
I call WebView.clearHistory(), but I am still able to go back after doing so. I want to reuse a WebView, but I don't want the back button to allow the user to go back further than the current "session" of using the WebView. Anybody know what is the best way to handle this? I thought for sure that clearHistory() would do it.
View 3 Replies View RelatedIn my android application i have a video list screen,a buffering screen and a videoplayer screen.
As soon as the user clicks an item from videolist screen ,he is navigated to buffering screen and then to videoplayer.
In the buffering screen i am using Async task and doing the loading process in on background process.
Now my issue is like the user if has selested a video then gets navigated to buffering screen.But if the user clicks back when he is in buffering screen then initially he is getting navigated to videolist screen but immediately after that again he is navigated to videoplayer screen.
What i would like to have is either the back button should be made disable in buffering or should prevent the player tostart if back is clicked.
In My code I am displaying a AlertDialog based on some condition.
CODE:.......................
I'm finding that I'm using the back button A LOT whether browsing the web or just navigating the OS. It kind of sucks because the buttons on the droid x are well, really small, and sometimes hard to press because they are so narrow and flimsy feeling. And depending on how you are holding the phone, it could be a good bit of distance for your thumb to travel or I'm finding myself having to reposition the phone in my hands just to reach the back button, which you seem to need very frequently.
Anyone know of any kind of back 'swipe' implementation ? Like some software you can install that makes it if you swipe left to right on maybe the top bar it goes back?
Instead of trying to grab the notification bar and swiping it down, simply start ABOVE the screen and swipe down. Fast or slow makes no difference.
You can swipe it back up, or simply hit the back button.
In the last few days I am having to press the back button twice when using both the stock android browser and the latest version of chrome on my rooted S3.
View 4 Replies View RelatedOn pressing the back button, I'd like my application to go into the stopped state, rather than the destroyed state. In the Android docs it states: not all activities have the behavior that they are destroyed when BACK is pressed. When the user starts playing music in the Music application and then presses BACK, the application overrides the normal back behavior, preventing the player activity from being destroyed, and continues playing music, even though its activity is no longer visible
How do I replicate this functionality in my own application? I think there must be three possibilities.
1) Capture the back button press (as below) and then call whatever method(s) the home button calls...........
Trying to the do as the title describes with the following code, but it doesn't seem to work. It just does nothing once i have altered the keyCode. Do i have to do something to the event?
..............
This has been brought up before, but I was unable to find a satisfying answer. And I know it's a newbish question.
What is the difference between exiting an app with the back button compared to the home button? I am not talking about switching to the home screen, I am talking about a scenario when you want to exit an app. For example, you play a game, you're done - do you press home or back? Or, you're in the Facebook app and you want to exit to the homescreen - what do you press - back or home? At first, I thought both buttons do the same with the difference that home brings you to the homescreen instantly while back goes through your previous steps/pages/screens in the current app and then returns you the homescreen.
But later I noticed that 'reactivating' some apps that were exited via the back button cause them to 'reload' (the loading screen shows up - with certain games, grooveshark's loading screen, etc.) while they switch back instantly when "exited" with the home button.
I gather the way to 'leave' an app you don't plan on using for at least a short while (I am not talking about closing the app, I know it's up to Android, or some task-killer) is the back button, while home is more for task switching/starting new apps while old ones are running.
I want to get the Text data value from Sub-Activity back. And everything is ok. But when the Sub-activity was opened, then I just click back "button" on the phone, it throw an exception error. I found on the android NotePad life-cycle control toturial but It's hard to understand. Can someone help me? This is my code:
public class SBooks extends ListActivity {
private String title_raw;
private SBooksDbAdapter mDbHelper;private static final int ACTIVITY_SEARCH = 0;private static final int SEARCH_ID = Menu.FIRST; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sbooks_list);
mDbHelper = new SBooksDbAdapter(this);
mDbHelper.open();
Create Menu Option
@Override public boolean onCreateOptionsMenu(Menu menu) {
boolean result = super.onCreateOptionsMenu(menu);
menu.add(0, SEARCH_ID, 0, R.string.menu_search);
return result;
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case SEARCH_ID:
Intent intent = new Intent(this, SBooksSearch.class);
startActivityForResult(intent, ACTIVITY_SEARCH);
return super.onOptionsItemSelected(item);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
super.onActivityResult(requestCode, resultCode, intent);
switch(requestCode){
case ACTIVITY_SEARCH:
Bundle bundle = intent.getExtras();
title_raw = bundle.getString(SBooksDbAdapter.KEY_TITLE_RAW);
if(title_raw!=null){
Cursor cursor = mDbHelper.searchData(title_raw);
String[] from = new String[]{ SBooksDbAdapter.KEY_ROWID,
SBooksDbAdapter.KEY_TITLE, SBooksDbAdapter.KEY_LYRICS };
int[] to = new int[]{ R.id.id, R.id.title, R.id.lyrics };
SimpleCursorAdapter adapter =
new SimpleCursorAdapter(this, R.layout.sbooks_row, cursor, from, to );
setListAdapter(adapter);This is my Sub-Activity:
public class SBooksSearch extends Activity {
private EditText mTextSearch;
private Button searchButton;
private SBooksDbAdapter mDbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sbooks_search);
mTextSearch = (EditText)findViewById(R.id.text_search);
searchButton = (Button)findViewById(R.id.btn_search);
searchButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent intent = new Intent();
intent.putExtra(SBooksDbAdapter.KEY_TITLE_RAW, mTextSearch.getText().toString());
setResult(RESULT_OK, intent);
finish();
@Override
protected void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
@Override
protected void onPause(){
super.onPause();
checkState();
@Override
protected void onResume(){
super.onResume();
}private void checkState() {
This is Logcat:
08-12 18:44:39.211: WARN/InputManagerService(581): Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@43719140 (uid=10004 pid=623)
08-12 18:44:39.510: INFO/ActivityManager(581): Displayed activity com.original.sbooks/.SBooks: 4934 ms
08-12 18:44:40.821: WARN/KeyCharacterMap(730): No keyboard for id 0
08-12 18:44:40.821: WARN/KeyCharacterMap(730): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
08-12 18:44:41.412: INFO/ARMAssembler(581): generated scanline__00000177:03515104_00001A01_00000000 [ 64 ipp] (89 ins) at [0x297608:0x29776c] in 952924 ns
08-12 18:44:41.520: INFO/ARMAssembler(581): generated scanline__00000177:03515104_00000A01_00000000 [ 46 ipp] (70 ins) at [0x297798:0x2978b0] in 517061 ns
08-12 18:44:42.171: INFO/ActivityManager(581): Starting activity: Intent { comp={com.original.sbooks/com.original.sbooks.SBooksSearch} }
08-12 18:44:52.196: WARN/ActivityManager(581): Launch timeout has expired, giving up wake lock!
08-12 18:44:52.279: WARN/ActivityManager(581): Activity idle timeout for HistoryRecord{436bfc80 {com.original.sbooks/com.original.sbooks.SBooksSearch}}
08-12 18:44:57.350: DEBUG/dalvikvm(620): GC freed 4103 objects / 231808 bytes in 75ms
08-12 18:45:05.130: DEBUG/dalvikvm(649): GC freed 2604 objects / 150112 bytes in 88ms
08-12 18:45:10.120: DEBUG/dalvikvm(623): GC freed 2750 objects / 149592 bytes in 71ms
08-12 18:45:11.920: INFO/ActivityManager(581): Displayed activity com.original.sbooks/.SBooksSearch: 29748 ms
08-12 18:45:17.263: WARN/ActivityManager(581): Activity pause timeout for HistoryRecord{436bfc80 {com.original.sbooks/com.original.sbooks.SBooksSearch}}
08-12 18:45:26.762: WARN/ActivityManager(581): Launch timeout has expired, giving up wake lock!
08-12 18:45:27.272: WARN/ActivityManager(581): Activity idle timeout for HistoryRecord{435a7760 {com.original.sbooks/com.original.sbooks.SBooks}}
08-12 18:46:04.905: DEBUG/AndroidRuntime(730): Shutting down VM
08-12 18:46:04.905: WARN/dalvikvm(730): threadid=3: thread exiting with uncaught exception (group=0x4000fe70)
08-12 18:46:04.905: ERROR/AndroidRuntime(730): Uncaught handler: thread main exiting due to uncaught exception
08-12 18:46:04.991: ERROR/AndroidRuntime(730): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=0, data=null} to activity {com.original.sbooks/com.original.sbooks.SBooks}: java.lang.NullPointerException
08-12 18:46:04.991: ERROR/AndroidRuntime(730): at android.app.ActivityThread.deliverResults(ActivityThread.java:3005)
08-12 18:46:04.991: ERROR/AndroidRuntime(730): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3047)
08-12 18:46:04.991: ERROR/AndroidRuntime(730): at android.app.ActivityThread.access$2300(ActivityThread.java:112)
08-12 18:46:04.991: ERROR/AndroidRuntime(730): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1721)
08-12 18:46:04.991: ERROR/AndroidRuntime(730): at android.os.Handler.dispatchMessage(Handler.java:99)
08-12 18:46:04.991: ERROR/AndroidRuntime(730): at android.os.Looper.loop(Looper.java:123)
08-12 18:46:04.991: ERROR/AndroidRuntime(730): at android.app.ActivityThread.main(ActivityThread.java:3948)
08-12 18:46:04.991: ERROR/AndroidRuntime(730): at java.lang.reflect.Method.invokeNative(Native Method)
08-12 18:46:04.991: ERROR/AndroidRuntime(730): at java.lang.reflect.Method.invoke(Method.java:521)
08-12 18:46:04.991: ERROR/AndroidRuntime(730): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
08-12 18:46:04.991: ERROR/AndroidRuntime(730): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
08-12 18:46:04.991: ERROR/AndroidRuntime(730): at dalvik.system.NativeStart.main(Native Method)
08-12 18:46:04.991: ERROR/AndroidRuntime(730): Caused by: java.lang.NullPointerException
08-12 18:46:04.991: ERROR/AndroidRuntime(730): at com.original.sbooks.SBooks.onActivityResult(SBooks.java:111)
08-12 18:46:04.991: ERROR/AndroidRuntime(730): at android.app.Activity.dispatchActivityResult(Activity.java:3595)
08-12 18:46:04.991: ERROR/AndroidRuntime(730): at android.app.ActivityThread.deliverResults(ActivityThread.java:3001)
I was wondering how or if it is possible to exclude activities from the back buttons history list? For example not letting the user back into the Splash Screen?
View 2 Replies View RelatedI want to implement back button functionality in my Android Application similar to the back button in the Browser but without using Intents.
I m going to view a sport listview, in that i click football listview , in that i click my favorite player listview, suppose if the user press back button in football listview then i have to diaplay sport listview.
I have a bunch of activities that are launching each other through intents. somehow, i got into a code state where some of the screens the back button (physical button) works, but others it does not.
I didn't do anything intentionally to disable the back button - what could be causing the back button not to work?
on the ui i am having a back button displayed. there are three activities say A,B,C. Activity A is splash screen. Activity B is home screen. Activity C is edit screen. on edit screen only i am having a back button. when the user press that i open the activity B which also contains some sort of http request and response. i want that the previous activity from stack should get displayed? how can i achieve that? i am able to open previous activity on pressing the back button of device? when i press back button on device there doesnt goes any http request? i want to achieve this behaviour when i press the ui back button.
View 2 Replies View RelatedMy app chains some activities.
if you press the back button, you go back through old activities then you suddenly quit the application !
so I need to show a message like "do you really want to exit" if it's the last activity on stack
I know how to override the back button but i can't figure how to know how many activity are in history
CODE:..................
I'm writing an application which will have two Activities, when the user presses the back button on the second activity a dialog should pop up asking the user to confirm the action. So how do I intercept this? I seriously doubt about this coz the backstack is a part of the OS itself. Has anyone found a workaround?
View 2 Replies View RelatedI have an activity that calls the
CODE:.........
I would like to add a button to the notes_row.xml that displays at the bottom of the screen "on top of" the actual notes_row allowing the user to still scroll through the list but hit the button if they want. Every time I try to change the layout of notes_row, I get the button on each item in the list. I am pretty new to Android and I think I need to inflate the button somehow so that it is layered in front of the list but doesn't impede the list, how do I go about doing that?
am new to this android development. I have index page and am displaying every links in the same page using div concept.Now i want to use the back button for my application i have developed code to handle the div and it is working in the AVD(Andriod Virtual Device 5556) but if i upload the apk into my device it is not working.
View 1 Replies View Relatedwhat the default behaviour of the back button is, as far as the Android platform is concerned.
For example, in the Contacts Application, if I am creating a contact, I just key in a name, and the press back button, the contact is saved with only the name. Fair enough. But, I then edit this contact to include a phone number and press back button. This contact is updated which now includes the phone number. I again edit this contact. This time I remove the name, as well as the phone number and press the back button. I get a message saying, "Contact doesn't exist". In this case, it deletes the contact. :(
In the mail appllication, while composing, when I press back, it goes to the drafts folder. Thats good. But, if I just have to cancel this, and I don't want this to go to drafts folder. For this, I have to open the menu, and select "Cancel".
Are there api to support go to back Programmatically i.e same effect as the back button of the phone.
View 5 Replies View RelatedI have a link in my application that triggers a webview to open from webview.loadUrl. Sometimes it's takes quite a while to load the page, and in these cases I want to be able to use the built in back button on the phone to cancel the loading and go back to my application. How do I do this? Is there some way I can listen to the back button while loading the URL?
View 1 Replies View RelatedI wanted to know if it is possible to Detect in an Activitiy's OnResume if the Activity is resuming because the devices Back button was hit or not?
View 5 Replies View RelatedI am starting an external activity from my application and would like the back button to return the user to the point in my application where they started the activity. is this possible?
View 7 Replies View RelatedI have a message application so that on view 1 is displayed a list of messages and when you go to view 2 (subactivity) you can send a message. I want the program to work so that when you hit the android back button, you can go back to view 1 and see the list of messages including the new message. Right now only the old data is displayed, and I noticed that displayContent() is not called when going back which I assumed it would. Is there any way to specify that when the back button is hit, to call displayContent() again on the previous activity?
View 3 Replies View RelatedI want to handle the back button in customize way. What to do.
View 3 Replies View RelatedIn my game, I've got one activity starting another activity via an intent. When the user is finished with the second activity, they quit it using the back button. However, I have a problem: when this happens, the second activity is still running, even if it's quit with the back button.
My second activity will eventually give a "game over" message using a pop up window if there is no user input for a period of time. Problem is, when I back out of the second activity (where the game is happening) the "game over" message still appears eventually. This is how I know the second activity is still running. It even displays a "game over" message if my entire app is quit (on the Android OS).
How do I stop an activity completely when the user presses the back button?
I have an application with a single Activity implemented as a singleton (in the AndroidManifest). It implements a Listener I wrote. When the application is first run the Listener works fine. When I use the "home" button to minimize it, then open it again it's still fine.
But when I press the back button then run it again from the application icon, the Listener doesn't work fully. The code runs and the Listening methods are called as usual but none of the UI elements that are listening update. It requires a restart of the phone to get the listener working again after this point.
I know the "back" button doesn't really close the application, per se. But resetting the phone sure does! Is there a way to use the onDestroy() or similar method to call a function that actually closes the application? I'm asking because restarting the phone is the only way to get the listener back so I was hoping I could do that programmatic ally.
For example, my progress bar is a member variable of my Activity, but onCreate() I do "progressBar = (ProgressBar) findViewById(R.id.progress_bar);" Is this perhaps creating a separate instance of it or something?