3

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?

Zhaidarbek
  • 310
  • 2
  • 12

1 Answers1

2

but i wonder if i am doing something wrong

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.

Could you please suggest best practice for this scenario?

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 from onCreate()) and one that fills in the data in the widgets in the tabs (called from onResume()).

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491