I am asking how can my activity get notified that the back button has been pressed.
I know that I can override onBackPressed or onKeyEvenet accroding to this blog post
but i think this is not optimized way to do so, since I have to restart the previous activity my-self while the OS is already doing this for me.
I just need to be notified that back button is pressed to set a flag to be true where I can use this flag later to know if the hardware back button has been pressed or not
but i think this is not optimized way to do so, since I have to restart the previous activity my-self
This is the best way to override the back button I know of, however that blog doesn't explicitly say you can call the super method... Have you tried this?
#Override
public void onBackPressed() {
// Set your flag here
super.onBackPressed();
}
Related
I'd like to do certain, simple action every time that my app is closing - I just want to decrease int variable. How to do that?
As #Ozair has rightly asked that it depends what do you define as closing of app.
You can do either or both of the following depending on the need:
If you want to detect closing of app by use of BACK button, then from within your last activity, you can detect the pressing of BACK button by overriding onBackPressed function. There you can decrement your value.
If you are also considering the situation when you app goes into the background by pressing of HOME button, then in your activities you would have to detect the HOME button pressed. There have been many solutions which no more work for detecting HOME button but this answer on How can I detect user pressing HOME key in my activity? question seems to work for me.
So, there you can detect the HOME button and decrement the value which you can save in SharedPreferece.
There can be other cases where you are calling finish() and closing your last activity. It is not clear from your question if you are considering that case as well.
Hope this gives you some opportunity to think about it.
The question is what you mean "close"?
If you close all your Activities, the App-process might still be running. If you mean that the "close" is just closing all of your Activities. You might define a "count" for all opening Activities, you can store it in DB or SharePerference. I think you can do follow(dummy codes):
In your project, you should define BasicActivity:
public class BasicActivity extends Activity {
onCreate() {
mPreference.incActivityCount();//++
super.onCreate();
}
onDestory() {
mPreference.decActivityCount();//--
if( mPreference.getActivity() == 0 ) {
//All being opened Activities have been closed.
onAppHasNoUIs();
}
super.onDestory();
}
onAppHasNoUIs() {
//All being opened Activities have been closed.
}
}
I have an app that consists of two activities: the "main" activity (M) and the "settings" activity (S). S can only be launched from M. The purpose of S is to modify settings which affect M. S has a "done" button that finishes the activity and goes back to M (through M's onActivityResult method, with an Intent that contains the new settings for M to apply).
My problem is: if I go back from S to M using the hardware "back" button (instead of S's "done" button) the system brings M to the top without any knowledge of the modified settings. Can this behaviour be modified? Ideally I would like to alter the behaviour of the hardware "back" button when S is on top, so that it cause S to finish (that way M would get the new settings).
If that's not possible, and more generally: what would you do you to have the settings applied on a "back" button pressing?
You can simply override onBackPressed()
#Override
public void onBackPressed()
{
// check if settings have been changed
super.onBackPressed();
}
Since this is a "closing action" do the super call after you have done your other work.
Following up on comments left on blackbelt's answer (now deleted comments) you may want to consider, if you haven't already, asking the user if they are sure they want to exit without saving in case they went into settings and decided not to change anything. What if they press the back button because they decided not to save the changes? You may already have something in place for this like a cancel button.
you have to override the onBackPressed from Activity and manage the same logic from the done button
You can also introduce a new java class to your package with static fields holding your settings.
Write to them as user changes settings & read from them as soon as in Activity's OnResume() method or later as needed.
You can achieve what you want by overriding onbackpressed method
#Override
public void onBackPressed() {
Intent intent = new Intent();
//get your settings from your views
intent.putExtra("setting1","on");
intent.putExtra("setting2","off");
setResult(RESULT_OK);
finish();
}
The answers above will do what you want, however:
Have you looked into using the built in android SharedPreferences? That way changes to the settings (made in S) will be stored to the device and then you can tell activity M to look at the settings and update appropriately in the onResume method. Plus the settings will be saved forever and it doesn't matter what happens to S.
I'm wondering how to handle sub processes with the back button in mind.
For example:
Skipping the login screen when clicking back from the default view
Pre-upload processing view. I want the back button to return to the last activity before the upload after the user uploaded or canceled.
I imagine the solution will include overriding the finish() behavior, I'm just not sure about the details.
Help will be appreciated.
Thanks
To prevent a previous Activity from appearing when the user uses the back button you should use the noHistory manifest flag.
I suggest you read up on how the Activity backstack works in more detail. This guide should prove useful:
http://developer.android.com/guide/topics/fundamentals/tasks-and-back-stack.html
First you just have override onBackButtonPressed()
Then from inside there you can perform whatever actions you wish.
If you want to kill you Activity and go back on the previous one on the stack you can just call finish(). There is no need to override it.
Check out this post for more information
http://android-developers.blogspot.com/2009/12/back-and-other-hard-keys-three-stories.html
Im connected to a chat server which gives me messages i print out in a textview. This continues when the user leaves the application by pressing the home key etc etc. I would like to close all streams if the user goes back with the phones back button to the previous activity. Problem is that onStop() and onPause() are both called independent of if the Home key was pressed or the back key. Its just called when the activity loses focus or visibility, doesnt matter which way it happened.
How do i find out if the back key was pressed, and not home?
You can implement your own version of onBackPressed(). That way you'll know every time it's pressed and can do what ever you need to in that callback. So do something like this:
public void onBackPressed(){
//Do the stuff you want to do
//Then call the parent class version function to allow it to do any other stuff
// that needs to happen as well.
super.onBackPressed();
}
I am not sure if there is a better way of doing this but I want to detect somehow what caused the application to pause.
On one particular activity that displays a tracking map if the user click back or home I want to stop GPS but if the screen goes off I want to keep the gps running I am also using a wake lock so it doesn't sleep (yes I know this probably should be in a service, but that will be v2 I'm running out of time!)
I am overriding when the back button is pressed
#Override
public void onBackPressed() {
wl.release();
this.mMyLocationOverlay.disableMyLocation();
this.mMyLocationOverlay.disableCompass();
if (mLocManager != null) {
mLocManager.removeUpdates(mLocListener);
}
super.onBackPressed();
}
but I can't find a way of doing the same for home.
Can anyone help?
Bex
in the onPause() you can call isFinishing() to know if the activity is finishing for whatever may be the reason or simply getting paused. See the doc here
But first please check whether your logic that you have written comes to onBackPressed(). write some log to confirm that that part of your code is active
If i understand you correctly. You have one activity which has some view from that activity you have started another activity. But you want when you click back key of the latest activity some actions you have to perfrom in previous activity. If this is the case you have to write logic in onResume() method. Because as soon as you click back key activity on top is finished and your focus comes to onResume() method of previous activity. donot override onKEyDown() or onBackPressed().
Donot forget to vote if you feel my response is useful for you.
Thanks
Deepak