Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top