質問

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top