In my Android application I need to Override the onResume() method to check which of two possible activities just finished. The user will either have entered an amount of money, or named and chosen a percent for a category. How can I do that? Also, if a user presses home and then goes back to my app, is onResume() called? If so, I can just call super.onResume(), right?
I have three classes: PaySaver, NewSavingCategory, and NewPaycheck. PaySaver.java is the main Activity, and there are two buttons: New Paycheck (launches a dialog box where a user inputs $ (NewPaycheck.java)) and New Saving Category (launches a dialogbox where a user inputs a name and a % (NewSavingCategory.java)). When the dialog box is closed via an enter button, I want the main activity to be updated with the information entered.
Thanks!
How can I do that?
Most likely, you don't. Both of those other activities updated your central data model. In onResume(), you update your UI from that same central data model. Hence, it does not matter where the user came from -- you are grabbing the latest data.
Also, if a user presses home and then goes back to my app, is onResume() called?
On the activity they return to, yes.
If so, I can just call super.onResume(), right?
Not only "can" you do that, you have to call super.onResume(), or your activity will crash.
Related
I want to know what are the pro and cons when you try to open a new activity with android and destroy the previous one straight away by calling finish.
People think that is a bad idea because Android can take care of the activity and drop them when there is too much memory used, but what about if I get inside that activity once and probably the user will never come back? Is this a bad option?
Also by finishing the activity, the history with the back button is "clear", so it wont get back to that activity ( only if your user flow needed to go back I think you should not call finish ).
And in terms of memory, is better to kill the activity with finish or leave android to have this activity in the background for who knows for how long time?
I feel like, that you kind of help the system to GC the activity that you closed and make sure that the user wont need to tap the back button 100 times before getting out of the application.
So what do you think? Better call Finish or not
I want to know what are the pro and cons when you try to open a new activity with android and destroy the previous one straight away by calling finish.
Either you want the user to return to the previous activity via the BACK button, or you do not.
If you want the user to return to the previous activity via BACK, do not call finish()
If you do not want the user to return to the previous activity via BACK, there are a multitude of options, depending upon where you do want the user to go when the user presses BACK
People think that is a bad idea because Android can take care of the activity and drop them when there is too much memory used
No, Android does not do this.
is better to kill the activity with finish or leave android to have this activity in the background for who knows for how long time?
It is "better" to have the activity implement onTrimMemory() and reduce its memory footprint as needed. Do not harm the user expectations of the BACK button.
that you kind of help the system to GC the activity that you closed and make sure that the user wont need to tap the back button 100 times before getting out of the application
Few users will "tap the back button 100 times". They will press HOME, or bring up the overview screen (a.k.a., recent-tasks list), or navigate to another app by other means.
Now, that being said, there will be times when you want to clear the task (back stack), again with an eye towards providing a logical flow for the user. For example, in an email app:
The user launches the app, and a fresh task is created, with the user going to the app's launcher activity, which shows the messages in the user's inbox (A)
The user taps on a "search" action bar item, bringing up a search activity, where they can search by various criteria (B)
The user fills in search criteria and clicks the "Go!" button, which does the search and shows matching email messages (C)
The user taps on an email message, bringing up an email-viewing activity (D)
The user taps a "delete" action bar item, which should delete the message and return the user... somewhere
If you believe that the user should return to the search results (C), you could call finish() in D. If, however, you believe that the user should return to the inbox (A), you would call startActivity() on A with appropriate flags (e.g., Intent.FLAG_ACTIVITY_CLEAR_TASK), to clear out the back stack and return the user to A.
In sum: do NOT call finish() to deal with heap space; implement onTrimMemory() instead. However, if navigation calls for finish(), then use it.
The following method:KeyChain.choosePrivateKeyAlias creates system dialog which allows user to choose a key alias from a list. As one of the arguments it takes Activity context which will be used to spawn this dialog. When selection is made it will call a callback.
In my scenario user doesn't select any alias but presses "Home" button. I would like to intercept this event. How can I do that?
Also I would like to cancel this dialog programatically. In this case I need some way to access this child Activity (please note that choosePrivateKeyAlias doesn't return any "handle" to the new dialog). Is it possible to access child Activity without any references (handle/id/etc.) to it?
There is no way to programatically end that activity because it is a system activity. It's the same as launching the browser on your device or the contact list. The only way to exit it is to press back and it will close that activity and resume yours.
According to my understanding, if you want to dismiss that dialog. Then as far as I know about Android OS. There are lifecycle methods for Activity. So I think
IF YOU CALL "yourDailog.dismiss()" INSIDE YOUR ACTIVITY's "onStop()". SO YOUR PROBLEM WILL BE SOLVED
If you do it in above way, so whenever you press HOME button, It will call onStop method of your activity. At that time, It will dismiss that dialog.
I am working on an App for collecting Customer Details like personal, business, assets etc.
The app shows a ListView with options and based on options Activities are started. After entering details in personal user presses back button and returns to MainActivity (Activity with a ListView) and selects another option like business details and assets detail. User enters the details and come back to MainActivity by Back button and selects another option.
I tried to save my Activitys using onSavedInstance(Bundle) and onRestoreInstanceState(Bundle) and sharedpreference but failed.
Can anyone help? I will provide code if required.
OnSaveInstanceState()/OnRestoresInstanceState() is not supposed to be used on a back key pressed from an Activity. In fact , OnSaveInstanceState() is never called. Here the user is explicitly destroying the Activity and removing it from the back stack.
These two methods are used in case where user presses home button or in cases where Android System destroys your Activity.
The options you can try
Try using StartActivityForResult()/OnActivityResult() methods. Here the target activity can set data whcih can be accessed by your parent activity.
Use an application class(a single instance class (singleton class) for holding data).
Things like shared SharedPreferences, files, database, etc.
I am stuck into this problem,please suggest a solution,
In my application user has settings page (activity) from where they can change profile data like name,profile photo etc..,
Now after user updates his information and comes back to home activity there is still old data is showing up, how can I update this information when user comes back to home.
You could override the protected void onResume() method of your Home Activity and fetch the data there (instead of in the onCreate i guess). This way it gets updated everytime you revisit this activity
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