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.
Related
I have an activity in my android app that calls an asynctask on onCreateView()
Here is my code :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_weeklyprofit);
fromDateTxt=(TextView)findViewById(R.id.fromDate);
toDateTxt = (TextView)findViewById(R.id.toDate);
GetXYPoints getXY = new GetXYPoints();
getXY.execute(fromDateTxt.getText().toString(),toDateTxt.getText().toString());
}
now my application needs to rotate, so i need to store data when rotate :
I have implemented this as below :
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putString("from", fromDateTxt.getText().toString());
savedInstanceState.putString("to", toDateTxt.getText().toString());
}
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
SystemClock.sleep(500);
fromDateTxt.setText(savedInstanceState.getString("from"));
toDateTxt.setText(savedInstanceState.getString("to"));
GetXYPoints getXY = new GetXYPoints();
getXY.execute(fromDateTxt.getText().toString(),toDateTxt.getText().toString());
}
So i recall the asynctask again with restored data, my problem is that the activity run again when rotate and the it calls onRestoreInstanceState so how can i prevent the activity from calling the first asyncktask?
In other way what is the best solution to store data returned by an asynck task when screen is rotate?
i would suggest you to to make sure you actually need your activity to be reset on a screen rotation (the default behavior). Every time I've had issues with rotation I've added this attribute to my tag in the AndroidManifest.xml, and been just fine.
android:configChanges="keyboardHidden|orientation"
source How to handle an AsyncTask during Screen Rotation?
In your AndroidManifest.xml add following for prevent reloading activity when orientation change
android:configChanges="orientation|screenSize"
So, you'll have something like this:
<activity android:name="Activity"
android:configChanges="orientation|screenSize">
</activity>
Hope it works!
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.
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.
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
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.