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...
Related
i'm trying to show an add on my app .
i want to show the add every time that the user launches the app or if the app is in the background when the user relaunch it.
i added the function that shows the add in the onRestart() event and it seems to work fine.
my problem is that if the user goes back to a previous activity by pressing the android Back button it triggers the onRestart() event as well.
is it possible to determine if the activity was loaded by the back button?
Thanks alot
Avi
From what I understand, here is what you can do:
If you are starting an activity from another activity, instead of calling startActivity(), use startActivityForResult(). In the called Activity, override the onBackPressed(). Put some data in the returned intent and call super.onBackPressed(). In the caller activity you will then analyze this to know that back was pressed :)
Also, move the advertisement to the onResume() method which is called when the Avtivity becomes visible to the user.
I suggest you to put your add in the Onresume() method of your main activity;)
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 have a small problem with a webview I'm using. I'm trying to use the webview to allow the user to fill out a registration form. This works fine when the user completes the form in one session but should the user lock their phone, the webview activity is destroyed.
I have two activities in memory, one of which is quite memory intensive. The problem I believe I'm having is that because this other activity requires more memory than the webview, the webview is destroyed.
I'm fine with this to a degree, but I would really like to be able to save the current state of the web page so the user doesn't have to submit all of their details again. Is there anyway I can do this.
Thanks
try it :-
You can use WebView method saveState()/restoreState() and save it to activity onRestore bundle and use saved value in onCreate method if the previous Activity will be destroyed.
You can use similar method which is described here (but used for only configuration change):
http://www.devahead.com/blog/2012/01/preserving-the-state-of-an-android-webview-on-screen-orientation-change/
We have run into a similar issue. We have an activity with a webview.
We want the user to input 3 fields on a form in the web view, then launch a new activity (camera) to take a picture. When we get the picture back, and using javascript we insert the picture into the currently active webview form. It all works great 99.9% of the time. However, in some odd scenarios which are difficult to reproduce, while the camera activity is active, the webview activity behind it gets destoryed. saveState / restoreState only partially restores the webview. The 3 form fields that the user had inputted but not submitted when we launched the camera are now lost. They do not get saved by webview.saveState. Also, some javascript state on the page is lost too.
To work around this we added a javascript method to the page and invoke it from the app on saveInstanceState. Then after we restore the state, we invoke another javascript method on the webview page to put the state back. It is a pain in the but and requires work for each form you need to do that to, but it works.
I hope future versions of Android webview.saveState save it exactly as it was before the activity gets destroyed. Alternatively it would be great if you mark an activity as critical to your app and only allow that activity to be destroyed if your app is not the current app the user is working with.
try WebView.addJavascriptInterface, when Activity.onPause, call javascript method in page by loadUrl("javascript:..."), and in web page, call native method that provided by WebView.addJavascriptInterface to save data. is this what you want?
An activity can be destroyed when back key is pressed; when the screen is rotated (is faster destroy the activity and recreate that resort all the graphic elements); or when is posible that it will not active in a few time (lock the phone, press home key or other application is started).
You need to do that #Ixx suggest in his comment : Android webView saveState
Save the data of your application with something like
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
/// save your things, e.g.
outState.putString("name", name);
}
And recover, it exists, in the method onCreate
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// if it is a new activity
if (savedInstanceState == null) {
// initialize vars, e.g.
name = "Unknown";
}
// if is a call after detroy the activity
else {
// recover vars, e.g.
name = savedInstanceState.getString("name");
}
}
This is a very basic question, I have a few screens, now when you go from one to another you can then press back and cycle back through all the windows.
I'd rather that when you pressed back it took you to a specific window for instance:
Menu Screen
---->User clicks Info
Info Screen
---->User clicks Ride Info
Ride Info
---->User clicks back
Info Screen
Now is this to do with the hierarchical parent, will this define where it goes back to?
The second part of my question is if I don't have any resources to release or information to store for an on-resume what should I do when the user pauses my app? At the moment if you go back to the menu screen and re-select the app it will start a new instance rather than resuming. Do I just simply implement:
#Override
public void onPause() {
super.onPause(); // Always call the superclass method first
}
and
#Override
public void onResume() {
super.onResume(); // Always call the superclass method first
}
Apologies if this is a bit basic!
You might want to look in to setting FLAGS for your intent while opening new activity Android Dev
Something like this -
Intent a = new Intent(this,A.class);
a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(a);
There is no basic questions here :)
Easiest way to do this is to override the onBackPress() function.
Sample :
#Override
public void onBackPressed() {
//Do what you want here
}
For saving variables when users leave the app, you need to override onSaveInstanceState(Bundle bundle)
#Override
protected void onSaveInstanceState(Bundle bundle) {
super.onSaveInstanceState(outState);
bundle.putInt("sample", 1);
}
For your two parts:
1) It's almost always best to let Android handle the back button, the order of which is determined by the back stack: see here for an explanation. If you want hierarchical navigation, I would recommend looking into the up button - see this developer page for a good explanation of how to use the different navigation tools.
Additionally, if you don't want to have an activity appear in your back stack, you can set the attribute android:noHistory="true" in your manifest, which will mean that the user can't return to it using the back button.
2) If the user has left your app, it's automatically paused, you don't need to implement onPause or onResume for this to happen. However, it's also up for collection to be terminated by the OS. If this happens, then it will be restarted when the user opens it from the launcher again. Any previously running instances of the app should automatically be opened.
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.