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();
}
Related
i have two simple questions (hope so).
First i want to close my app, when the user hits the homebutton in my HomeFragment, because there is a counter (Days,Hours) and when I click the home Button and come back later there is still the old value.
Then i have to close the app via backbutton and start again. So i want my app to get killed, when I hit the home button.
Then i have a button to delete the last value, on which the counter is based. When I hit the button i want to run my homeFragment again. Should i just create a funnction which calls, the homefragment.onCreate again?
When I hit the button i want to run my homeFragment again. Should i just create a funnction which calls, the homefragment.onCreate again?
No, these functions are meant to be called by the system.
You are probably looking for the onStop() method (also called by the system)
protected void onStop()
{
super.onStop();
// clear counter e.g.
myCounter.setText("");
}
Other than this, it's kind of hard to tell what you want. Feel free to add more (if not all) of your code.
I have an app that opens a web view and uses a javascript interface to play audio on my Android App. I want the app to close if you hit the back button, which should be happening automatically, but it's not.
The only 2 activities I have are the MainActivity and the WebAppInterface. It's possible that the back button is closing the main activity, but the WebAppInterface for the web view (that also plays the audio) isn't closing when the back button is pressed, but I'm not sure.
Any advice? I would post the code, but there's nothing to see here. A note, I never call
finish();
anywhere in the app, and maybe I should?
I want the app to close if you hit the back button
What your users want is for the music to stop when the user hits the back button. Presumably, you will do that by the same sort of mechanism that you used to start the music in the first place.
A note, I never call finish(); anywhere in the app, and maybe I should?
This happens automatically when the BACK button is pressed, assuming you have not done anything to interfere with it (e.g., overrode onBackPressed() and failed to chain to the superclass).
You can can call finish when you start the WebAppInterface from MainActivity.
Like:
Intent i = new Intent(this, WebAppInterface.class);
startActivity(i);
finish();
This way when you call finish on the WebAppInterface, it won't go back to the MainActivity instead it will close like the MainActivity does.
I'm working on an Android app that will show college fitness professors how their students are doing in their classes. Since this data is fairly sensitive (biometrics are shown, including weight, something many college students are self-conscious about) I don't want the data to be available to anyone who picks up the tablet. While I have a proper login screen created, complete with authentication for the database, etc. I have an issue when the home button is pressed. Since Android doesn't close a program immediately on leaving the app, it's possible to reopen it and return to where you were. I would like to force the app to return to the login screen each time (I've altered onBackPressed so you can't just return to the previous view from the login screen) so that you have to re-enter your credentials to get back into the app. However, I can't seem to do this. An answer I found on here said to use the following line:
android:clearTaskOnLaunch="true"
However, no matter what XML file I put it in, be it the Manifest or the individual Activity XMLs, it appears to do nothing. So, how do I ensure the login screen comes up each time the app is launched, regardless of whether it is starting from scratch or not?
Try to play around with onUserLeaveHint() method. If you read its documentation, it says:
Its Called as part of the activity lifecycle when an activity is about to go into the background as the result of user choice. For example, when the user presses the Home key, onUserLeaveHint() will be called, but when an incoming phone call causes the in-call Activity to be automatically brought to the foreground, onUserLeaveHint() will not be called
So, when ever you detected home button pressed, you can finish the running activity/activities. So next time user click the app, it will start from the first login screen.
Hope this helps.
You should override onUserLeaveHint()
#Override
protected void onUserLeaveHint() {
// do your logic
//finish() or start login activity
}
You could set a flag when onPause() is initiated within the activity. And then when you return you could check the flag from within onResume() and then request a login from that point. This will be sure to request it each time; in a simple case of course.
Edit:
With multiple activities, you could check against a saved context to see if they are the same when you start a new activity. If the contexts differ then you can discard the context previous activities context and start a new activity.
If they are the same, then you have come back to the activity from itself (you have lowered and brought the screen back). You would have to use some form of saved state such as that to do it in this manner with multiple activities when outside the case of a simple application.
I found out how to do it in my case. For any others with the same problem, try following the example here:
Android detecting if an application entered the background
Can anyone point me out or explain what is the difference between android Home key and Back key and their respective behavior related to an android app/activity.
Thank you.
Back Key :
If you press Back Key, onPause(), onStop() and onDestroy() callbacks will called.
Activity will created again by system calls onCreate() callback, then onStart() and onResume() callbacks will be followed.
Home Key :
If you press Home Key, onPause() and onStop() callbacks will called.
Here Activity will restart by system calls onRestart() callback, then onStart() and onResume() callbacks will be followed.
In addition to #Fosco's comments, using back will usually cause an app to exit, where home will leave it running. This is dependent on the application, but the general pattern is to exit the app when using back on the last activity.
Back key destroys the current Activity, home key doesn't. In the Activity lyfecycle, pressing back calls all the way to current activity's onDestroy() method. On the other hand, pressing home pauses the Activity, which stays alive in background.
The home key takes you to the home screen, the back key takes you back to the previous activity (or home if there's no activity to go back to.)
If you are at the home screen and launch Messaging, then hit back, it's the same as hitting the home key.
If you're in Email and get an alert for a text message, and you choose the notification which takes you to Messaging, then hit Back, you'll go back to Email.
edit: as mentioned by Tim Coker, when the back button takes you to the home screen, it usually terminates the activity. I think this is based on the app, whether it terminates or stays resident.
I am still learning the ins and outs of Android development. I am playing around with the Notepad tutorial application to try and get different behavior.
Right now, I want to have the application do the following in the NoteEdit activity:
1) If the Back button is pressed, current state is ignored; basically, it's like an implicit cancel, and you are taken back to the list.
2) If the Home button is pressed, it takes you to the home page as normal. However, if you open the application again, it should go back into the NoteEdit activity in the same state as when you left (IE, if you were partway through an edit, for example).
I removed the "saveState" stuff from onPause, because I don't want to store to the DB unless "Confirm" is pressed (instead, I moved the call to saveState to the confirm button). By doing this, hitting "Back" basically throws out your changes, which is what I want. However, going Home and coming back also throws out your changes, though it does remain in the NoteEdit activity. Both "Back" and "Home" cause the onPause message to trigger, and both cause onResume to trigger (either from clicking on the item in the "Back" case, or by going back into the app in the "Home" case).
Is there a way to have these two events handle saving the state differently? Is it possible to have the Home button store the state (temporarily), while not having the Back button do it?
Thanks in advance!
You need to define an onSaveInstanceState method, but instead of saving to the DB (as in the Notepad sample), save your Activity's state to the Bundle. You then need to recover from the saved state in your onCreate when the passed in Bundle is non-null.