Android: Why is onTabSelected called when creating tabs?

When there are tabs in the action bar, I noticed that onTabSelected is called when activity loads on the screen. It is also called whenever an orientation change occurs. My code queries the database depending on the selected tab and displays the query results in the loaded layout.

My problem is to save the tab state, and the currently selected tab is 1 or higher, in recovery state, since onTabSelected is called by default on tab 0, it will be called again when the restored status tab is 1 or higher. This makes the database query on tab 0 useless.

How to configure an android that onTabSelected is not called when creating a tab or at least detects that this call is by default and is not called by the user?

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    int tabPos = tab.getPosition();
    switch(tabPos) {
    case 0:
        // query database and display result
        break;
    case 1:
        // a different query and display result
        break;
    case 2: ...
    }
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    if(savedInstanceState.containsKey(STATE_SELECTED_TAB)) {
        getActionBar().setSelectedNavigationItem(savedInstanceState.getInt(STATE_SELECTED_TAB));
    }
    super.onRestoreInstanceState(savedInstanceState);
}

@Override
public void onSaveInstanceState(Bundle outState) {
    outState.putInt(STATE_SELECTED_TAB, getActionBar().getSelectedNavigationIndex());
    super.onSaveInstanceState(outState);
}

Added complication: When the currently selected tab is 0 and the orientation changes, onTabSelected is still called twice! Once, when the tabs are initially created, and the 2nd time onRestoreState restores the saved state of the selected tab, even if it is 0. Strike>

, onTabSelected , . onCreateView, , , . onTabSelected TabReselected , 0.


SO google, , . ActionBar selectTab

+4
2

, ActionBar Google Android, addTab onTabSelected.

public abstract void addTab (ActionBar.Tab tab)

. ​​ . , , .


  tab     Tab

, , onTabSelected.

public abstract void addTab (ActionBar.Tab tab, boolean setSelected)

, . , . , onTabSelected, onTabReselected.

+10

, ( :-)):

public class MainActivity extends FragmentActivity {

boolean isConfigChanged;
int savedTabIndex;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
     if(savedInstanceState != null){
        if(savedInstanceState.containsKey(STATE_SELECTED_TAB)) {
            savedTabIndex = savedInstanceState.getInt(STATE_SELECTED_TAB);
          //getActionBar().setSelectedNavigationItem(savedTabIndex); actually you do not need this
        }
        isConfigChanged = true;
    }

   // here add actionbar tabs

//...}

:

, configuration , , , not zero, , isConfigChanged == true , , 0, must query DB isConfigChanged == false , , . , .

   @Override
   public void onTabSelected(Tab tab, FragmentTransaction ft) {
    if(isConfigChanged && savedTabIndex != 0){
       isConfigChanged = false;
       return;
     }
    isConfigChanged = false;
    int tabPos = tab.getPosition();
    switch(tabPos) {
    case 0:
        // query database and display result
        break;
    case 1:
        // a different query and display result
        break;
    case 2: ...
    }
  }

onRestore

@Override
public void onSaveInstanceState(Bundle outState) {
    outState.putInt(STATE_SELECTED_TAB, getActionBar().getSelectedNavigationIndex());
    super.onSaveInstanceState(outState);
}
0

All Articles