I'm still working on my app, and I have a new issue.
On my WarikeActivity I have a button, this button send me to a MapActivity and then I want to get back to my WarikeActivy with some values from the MapActivity, I tried this:
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putByteArray("byte_warike", byteArray);
super.onSaveInstanceState(savedInstanceState);
}
And then of course
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
byteArray = savedInstanceState.getByteArray("byte_warike");
foto = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
imgFoto.setImageBitmap(foto);
}
But the app crash because byte_array is always null. Any idea about how to solve it?
I was reading others posts by I cant solve that problem yet.
Thanks in advance.
OnSaveInstanceState and onRestoreInstanceState should actually only be used when the activity is being recreated and wants to save some info about itself.
You, on the other hand, want to send info to the previous activity.
Reto Meier (a Google employee working on Android) has given a perfect response how this could be done here: How to pass the values from one activity to previous activity
Related
I'm moving from activity 1 to activity 2 and send some data via intent .
It's ok and no problem ,but when I leave activity 2 and then come back , the intent is empty . For solving this problem I've tried this code :
#Override
protected void onSaveInstanceState (Bundle outState) {
outState.putInt("row_id", row_id);
}
#Override
protected void onRestoreInstanceState (Bundle savedInstanceState) {
int row_idss = savedInstanceState.getInt("row_id");
Log.v("this","restore" + Integer.toString(row_idss));
}
I tried to save it before I leave the activity and retrieve it on re-open .anyway , It didn't work and didn't log it either.
I tried onPause and onResume , strangely they didn't call either . I used this code to make sure it runs
#Override
protected void onResume() {
super.onResume();
Log.v("this","test");
}
nothing logged .
Could you help me to solve this problem ? I don't know what should I do to solve it
thanks so much
You are missing the calls:
super.onSaveInstanceState(outState);
super.onRestoreInstanceState(savedInstanceState);
Check the answer of this question, in this answer they're not using the method onRestoreInstanceState but just the onCreate to restore the data. You also could use onRestoreInstanceState if you need a different behaviour.
Im developing an app that download tweets from twitter.
When my app change orientation, the activity loads again the tweets (it downloads again the same tweets).
So whats the best solution for a case like this ?
Is using android:configChanges="orientation|keyboardHidden|screenSize" a way to go ? I have read that is a bad practice.
Thanks
You can override onSaveInstanceState and onRestoreInstanceState to avoid downloading same tweets again.
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putSerializable(<Key>, <Value>);
super.onSaveInstanceState(savedInstanceState);
}
When the orientation is changed, restore the saved tweets.
[Edit]
Alternatively, you can do it without overriding onRestoreInstanceState:
public void onCreate(Bundle savedInstanceState) {
if (savedInstanceState!= null) {
value = savedInstanceState.getSerializable(<Key>);
}
}
More information here.
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);
}
As I say in the topic, I want my WebView to prevent from reloading the webpage when another activity comes to the foreground or just when the orientation is changed. The reason why is because the WebView content is generated by Javascript/AJAX almost completely. After searching on a couple of forums, I found out many people suggested to use "saveState" and "restoreState" methods, but when I look the documentation, it says:
Please note that this method no longer restores the display data for this WebView. See savePicture(Bundle, File) and restorePicture(Bundle, File) for saving and restoring the display data.
So, here I was using that savePicture and restorePicture as it follows:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
... some other lines ....
setContentView(R.layout.main);
mWebView = (WebView) findViewById(R.id.webview);
if (savedInstanceState == null){
loadInicialWebUi();
}else{
mWebView.restoreState(savedInstanceState);
boolean restoredPic = mWebView.restorePicture(savedInstanceState, new File("savedDisplayWebView.temp"));
Log.d(TAG, "restored Picture:" + restoredPic);
}
}
#Override
protected void onSaveInstanceState(Bundle savedInstanceState) {
mWebView.saveState(savedInstanceState);
boolean savedPic = mWebView.savePicture(savedInstanceState, "savedDisplayWebView.temp");
Log.d(TAG, "saved Picture:" + savedPic);
super.onSaveInstanceState(savedInstanceState);
}
And well, those logs were revealing that it saves the picture, but it couldn't restore it. I suspect there might be something about the File references, but I couldn't think of a better way of getting the reference of the File I created when saving the state.
Anyone thrilled?
I would appreciate any clues/suggestions.
Thanks in advance.
Manuel.
I would load the page in onCreate() and put this attribute on your Activity in the manifest:
android:configChanges="orientation|keyboardHidden"
This will prevent your Activity from being restarted when orientation changes. And since your Activity loads the page in onCreate(), bringing another Activity to the foreground won't cause you to reload the page.