I have a complex TabActivity that contains ListViews and TextViews. Instead of messing up with manual UI update I decided to “recreate” activity (force full redraw) whenever it comes to foreground. Assume that i navigate from activity A to B. When hit back on B, activity A must be recreated. Here is the code:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
setupTabs();
}
@Override
protected void onNewIntent(Intent intent) {
startActivity(intent);
finish();
}
@Override
protected void onRestart() {
super.onRestart();
onNewIntent(getIntent());
}
It works, but i wonder if i am doing something wrong, maybe there is a more elegant solution. Could you please suggest best practice for this scenario?
You are wasting CPU time and battery life. You are making the user experience worse by forcing the activity to be always recreated from scratch.
The best practice would be for you to perform a “manual UI update”. Divide your
setupTabs()into two pieces, one that truly creates the tabs (called fromonCreate()) and one that fills in the data in the widgets in the tabs (called fromonResume()).There may be more to it for your case, but since you elected not to explain what is so difficult about the “manual UI update”, I cannot really advise you further.