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
Related
I am calling a Master/Detail activity in android by clicking on a button placed in another activity (UserActivity). The strange thing is, that if i click the back button in the Master/Detail activity, I loose the Data in the state of the UserActivity. It wants to execute the onCreate Method again.
If I click on the login-button in the LoginActivity where i am redirected to the UserActivitiy and i go back with the Back-Button of the "Smartphone", the username and password i typed are still there. So there i do not loose the data.
Is there a difference between the back-button of the Smartphone and the back-button at the top of the program? I am a bit confused now and i know how to persist the state of the Activity. But my question is, why i am having this behavior on the one side and on the other not.
Just in case you will leave the question like this and don't add code:
What will probably help is checking for the amount of items that are already there. When I ran into this issue, the onStart() got called so quickly that it seemed to me that the Activity has lost the data. Actually it DID have the data, but calling onCreate/onStart (I'm in a Fragment) NULLed it.
What I did to avoid this is to check if there is a need to load items in the list. If there is, it calls a method that contains what the old onCreate/onStart did. If there is no need to load data, it will just skip the step and live with the "old" data happily for the rest of its life.
#Override
public void onStart() {
super.onStart();
if(DummyContent.ITEMS.size()<3){
initializeApp();
}
}
void initializeApp(){
videoTitles=new ArrayList<String>();
videoUrls=new ArrayList<String>();
. . .
}
protected override void OnResume()
{
base.OnResume();
//if the page comes from another Activity, don't need to do ShowCurrentPage() to refresh page
//if I press the home button, then the page comes from home page , need to do ShowCurrentPage() to refresh page
ShowCurrentPage();
}
what my question is, how to distinguish the origin from home page or Activity when I do the OnResume() to show Activity ?
And if I need to do different event in OnResume() according to different origin of Activity, if I must write if{} before ShowCurrentPage()
Wating for good answers, Thank you!
Xamarin Forms : Use Plugin.Settings to store value as local data in your device, base on this value to do ShowCurrentPage() method or not. As when page comes from home page, set value in OnStop()
Native apps : Use sharedpreferences...
My app begins with Activity A (log in screen) which I always want on the bottom of the Activity stack.
Based on some logic after login, I launch some other Activity B-Z. There is a menu in the app which allows you to switch around between any Activity B-Z.
If the user hits the back button enough times, I don't want them returned to the login screen. I want the user to be sent to the Android Home screen if the back button is pressed on the Activity which has Activity A as the next Activity on the stack. This Activity is not guaranteed to be the Activity which was launched by Activity A because my Activities use singleTop.
Ideas?
The only other option I can think of is to remove singleTop and whatever Activity is launched by Activity A could remember that (my Activities all derive from a base class, which I would use to do that).
Another possibility may be to do something like the following in the onBackPressed handler:
if (getParent().getClass().getName().equals(ActivityA.class.getName())) {}
Although not a direct answer to your question, but if the problem is that
I don't want them returned to the login screen
then the classic solution is to finish() the login Activity when the user has logged in successfully. This way you'll make sure the user will never return to that Activity.
To do this, Why don't you get the User Login information, and store it in your apps private shared preferences. Then when the User launches your application, you can read the Shared preferences from activity A, and automagically log them in to activity b .
something like this:
SharedPreferences myPrefs = getBaseContext().getSharedPreferences("MyPrefs", SharedPreferences.MODE_PRIVATE);
if((myPrefs.getString("username",null) != null) && (myPrefs.getString("password",null) !=null)){
// Make sure your user is a member of your application
// auto log in to the home page
Intent bIntent = new Intent(getBaseContext(), BIntent.class);
startActivity(bIntent);
}
For more reference on how to use Shared Preferences
http://developer.android.com/reference/android/content/SharedPreferences.html
Then if you want to be really slick, Store the Class name when the onDestroy() method is called in each Activity, overwriting each class as the last open Activity. So whichever activity the user was last on before the application was closed is stored in preferences and you can read that in the login activity, then from there launch b-z
This will make it so that the login activity is always in memory, since it is first launched to check your users credentials.
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
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.