If you create the app described in this tutorial http://developer.android.com/resources/tutorials/views/hello-datepicker.html
change the date, and then rotate the device, you will notice the following:
The app's textview date resets as expected (since the activity is destroyed and recreated), but if you then hit the "change the date" button, the date that you set prior to the rotation is retained. The SDK documentation discourages using onConfigChanged="orientation|screenSize" to catch the orientation change, so my question is: what is the "best" way to keep the datepicker's default consistent with the member variables that are used in one's app? I don't much care if the values of the member variables get retained, or if the picker gets reset to today's date along with them, but the inconsistency looks sloppy to me.
You can save your DatePickerDialog member variables in onSaveInstanceState, then restore them on onCreate. Something like:
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("day", mDay);
outState.putInt("month", mMonth);
outState.putInt("year", mYear);
}
To complete the above answer, you would probably do something like this, as shown here (override the two methods below in your Activity):
https://stackoverflow.com/a/54283814/5916188
#Override protected void onSaveInstanceState (Bundle outState)
{
super.onSaveInstanceState (outState);
outState.putInt ("YEAR", mDatePicker.getYear ());
outState.putInt ("MONTH", mDatePicker.getMonth ());
outState.putInt ("DAY", mDatePicker.getDayOfMonth ());
}
#Override protected void onRestoreInstanceState (Bundle savedInstanceState)
{
super.onRestoreInstanceState (savedInstanceState);
mDatePicker.updateDate (savedInstanceState.getInt ("YEAR"),
savedInstanceState.getInt ("MONTH"),
savedInstanceState.getInt ("DAY"));
}
Related
I have looked in all the site and I don't understand why it's not working on my side.
Please find my source code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
Log.e("EVERYTHING", "OK");
}
}
and
#Override
public void onSaveInstanceState(Bundle outState) {
outState.putString("test", "titi");
Log.e("Save", "titi");
super.onSaveInstanceState(outState);
}
I launch my appli, then press the home button, then restart my phone and launch the appli again, but the savedInstanceState is null.
Could you please help me.
Regards
onSaveInstanceState is used to persist state in memory so it's not suitable for situations like you described above. If you restart your phone, obviously all state store not persistently (only in memory) is not preserved.
If you want to store state this way, you should look at something persistent, e.g. shared preferences or SQLite.
public void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
outState.putInt("x", x);
}
When I save a Bundle, Where is the Bundle variable exactly saved? I can't find it anywhere
In addition to android reference, you can read the source code to understand what has happened here.
protected void onSaveInstanceState(Bundle outState) {
outState.putBundle(WINDOW_HIERARCHY_TAG, mWindow.saveHierarchyState());
Parcelable p = mFragments.saveAllState();
if (p != null) {
outState.putParcelable(FRAGMENTS_TAG, p);
}
getApplication().dispatchActivitySaveInstanceState(this, outState);
}
In the source code, it's clear to see the state will be managed by Application. When the Activity is destroyed, the Application can help save relevant states. But, if you ever met this situation that Application was killed, you would find all states were lost. So, I think all states are kept in memory, not file like preference.
Well, I don't think you will find it and I don't expect to be referenced directly somewhere.
However its content will be available in onCreate(savedInstanceState) when the activity is recreated. Taken from its documentation: savedInstanceState: If the activity is being re-initialized after previously being shut down then this Bundle contains the data it most recently supplied in onSaveInstanceState(Bundle). Note: Otherwise it is null
Another place to look for its content is onRestoreInstanceState(savedInstanceState)
i am not sure I think you must set the int,string or whatever you want
so as to save it into int
Let me show you an
EXAMPLE
public void onSaveInstanceState(Bundle state){
super.onSaveInstanceState(state);
Int i = 1;
state.putInt("s",i);
}
Iam little bit amazed with this.I have an onResume() in my activity.Its called and works well in my emulator, but in a physical device samsung galaxy note for specific with jellybean installed,its not called.Instead onCreate() is called all the time.Why this happens?
public void onResume(){
super.onResume();
if(firsttime){
try {
Toast.makeText(getApplicationContext(), "Resuming Activity",Toast.LENGTH_LONG).show();
addReminder();
} catch(Exception exception) {
exception.printStackTrace();
}
} else {
firsttime=true;
}
}
This is my code.firsttime is a static boolean variable.It is used to prevent onResume() being called when app is started for the first time
Considering your current scenario, you should save variable in preferences instead of relying on activities lifecycle since lifecycle depends on many things.
Using static variable for this scenario is bad choice in general.I think this should solve your problem.
Try to print something inside the onResume and check it in LogCat.... the code inside onResume may be causing this.
or else can you elaborate your question?
I think here is what happens,
when your app not the Top app, the activity manager actually destroy the activity, it only called
public void onSaveInstanceState(Bundle savedInstanceState)
no
onStop
called, so no
noResume
will be called.
The correct to do this is, when put all states of this activity when
public void onSaveInstanceState(Bundle savedInstanceState)
called.
and in your onCreate() function, do such thing
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // Always call the superclass first
// Check whether we're recreating a previously destroyed instance
if (savedInstanceState != null) {
// Restore value of members from saved state
mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);
} else {
// Probably initialize members with default values for a new instance
}
...
}
to check if you have some saved state.
Most code was copy from android developer site:
http://developer.android.com/training/basics/activity-lifecycle/recreating.html
I have an activity with action bar tab. Each tab contain a fragment. Now when I rotate my device, bundle in my corresponding fragment is coming as null. This is taken care when I using device post android 3.2, but it is happening when device is Andoird3.0. I am having a headache after working on this issue. I crossed check various link on SO, but no help. Although I have given enough details, still will provide some code snippet as at various cases user ask for code snippet.
In my fragment class I am storing this value
#Override
public void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
outState.putBoolean("textboxVisible", true);
}
this is storing one boolean variable which it retrived as below.
/**
* Function called after activity is created. Use this
* method to restore the previous state of the fragment
*/
#Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
if (savedInstanceState != null)
{
//restore the state of the text box
boolean textboxVisible = savedInstanceState.getBoolean("textboxVisible");
if (textboxVisible)
{
//do some stuff
}
}
}
but after rotation savedInstanceState is coming as null.
I don't what is going wrong. I have read in some document that below 3.2 the onCreateView() of
fragment is not called with bundle value. But to deal with this. Any help will be appreciated.
if you use setRetainInstance(true) the savedInstance bundle is always gonna be null after orientation changed. SO you cannot really save something with it, but what you can do if you need to save something, is to put it in a data member of the fragment, because setRetainInstance(true) preserves the fragment and doesn't destroy it, so after the device was rotated you gonna have the same values.
Try to get the savedInstanceState in onCreate of the Fragment.
Like
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
if (savedInstanceState != null) {
// IT MUST NOT BE NULL HERE
}
}
Please try... i hope it will work
I have implemented the Demo version of HelloDatePicker provided by the API.
http://developer.android.com/resources/tutorials/views/hello-datepicker.html
However I cannot maintain its value after rotating the screen. Can someone post an example or indicate how one can do this.
Thanks
Use onRetainNonConfigurationInstance() to save the date, and in onCreate use getLastNonConfigurationInstance() to load and set the date.
Even without a Fragment, the Activity alone can do this:
#Override protected void onSaveInstanceState (Bundle outState)
{
super.onSaveInstanceState (outState);
outState.putInt ("YEAR", mDatePicker.getYear ());
outState.putInt ("MONTH", mDatePicker.getMonth ());
outState.putInt ("DAY", mDatePicker.getDayOfMonth ());
}
#Override protected void onRestoreInstanceState (Bundle savedInstanceState)
{
super.onRestoreInstanceState (savedInstanceState);
mDatePicker.updateDate (savedInstanceState.getInt ("YEAR"),
savedInstanceState.getInt ("MONTH"),
savedInstanceState.getInt ("DAY"));
}
After rotation, Activity is recreated. The simplest way to avoid it is to add:
android:configChanges="keyboardHidden|orientation"
in AndroidManifest.xml in Activity declaration.
For more information you should read it.