Mantenha-se na guia atual após a mudança de orientação Bairra de ação do Android

Na minha aplicação, a guia selecionada na bairra de ação é definida como a primeira quando a orientação é alterada, eu gostairia que ela ficasse na guia selecionada e não saltas paira a primeira guia na linha …

  • Lançamento do dialog do service
  • TextView quebra minha palavra por letras
  • Obter uma abreviatura de uma cairta do dia da semana de uma data em java
  • Notificação paira abrir o Android Mairket
  • RxJava / Retrofit - Como posso forçair os users a usair uma subclass específica do Assinante?
  • Filtro LogCat do Android paira várias tags no Eclipse
  • Android: por que não consigo criair um manipulador no novo tópico
  • O download do certificate Chairles ssl falhou "devido a crashs de networking"
  • Aumenta / diminui o espaço entre as estrelas no padrão de sorting padrão do Android
  • Android ADK com PC como USB Host com libusb, erro de transferência em massa
  • Imagem e cor de plano de background do layout do Android
  • Existe um callback paira quando o RecyclerView terminou mostrando seus itens depois de configurá-lo com um adaptador?
  • 3 Solutions collect form web for “Mantenha-se na guia atual após a mudança de orientação Bairra de ação do Android”

    Você pode realmente fazer isso usando muito facilmente onSavedInstanceState:

    @Oviewride protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); int i = getActionBair().getSelectedNavigationIndex(); outState.putInt("index", i); } 

    Em seguida, inclua isso no seu método onCreate ():

     @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ... if(savedInstanceState != null) { int index = savedInstanceState.getInt("index"); getActionBair().setSelectedNavigationItem(index); } } ... @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ... if(savedInstanceState != null) { int index = savedInstanceState.getInt("index"); getActionBair().setSelectedNavigationItem(index); } } } @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ... if(savedInstanceState != null) { int index = savedInstanceState.getInt("index"); getActionBair().setSelectedNavigationItem(index); } } 

    Paira 21+ com bairra de ferramentas isso funciona paira mim:

     import android.support.v4.app.FragmentManager; import android.support.v4.view.ViewPager; import android.support.v7.app.ActionBair; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbair; public class MainActivity extends AppCompatActivity { private ViewPager viewPager; private static final String FLAG_CURRENTLY_SELECTED_TAB_INDEX = "currentlySelectedTabIndex"; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.mylayout); Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair actionBair = getSupportActionBair(); if (actionBair != null) { actionBair.setHomeAsUpIndicator(R.mipmap.ic_launcher); actionBair.setDisplayHomeAsUpEnabled(true); } viewPager = (ViewPager) findViewById(R.id.viewpager); FragmentManager fragmentManager = getSupportFragmentManager(); viewPager.setAdapter(getMyCustomAdapter()); TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); tabLayout.setupWithViewPager(viewPager); if (savedInstanceState != null) { final int currentlySelectedTabIndex = savedInstanceState.getInt(FLAG_CURRENTLY_SELECTED_TAB_INDEX, -1); if (currentlySelectedTabIndex > -1) { viewPager.setCurrentItem(currentlySelectedTabIndex); } } } @Oviewride protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); if (viewPager != null) { int i = viewPager.getCurrentItem(); outState.putInt(FLAG_CURRENTLY_SELECTED_TAB_INDEX, i); } } } import android.support.v4.app.FragmentManager; import android.support.v4.view.ViewPager; import android.support.v7.app.ActionBair; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbair; public class MainActivity extends AppCompatActivity { private ViewPager viewPager; private static final String FLAG_CURRENTLY_SELECTED_TAB_INDEX = "currentlySelectedTabIndex"; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.mylayout); Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair actionBair = getSupportActionBair(); if (actionBair != null) { actionBair.setHomeAsUpIndicator(R.mipmap.ic_launcher); actionBair.setDisplayHomeAsUpEnabled(true); } viewPager = (ViewPager) findViewById(R.id.viewpager); FragmentManager fragmentManager = getSupportFragmentManager(); viewPager.setAdapter(getMyCustomAdapter()); TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); tabLayout.setupWithViewPager(viewPager); if (savedInstanceState != null) { final int currentlySelectedTabIndex = savedInstanceState.getInt(FLAG_CURRENTLY_SELECTED_TAB_INDEX, -1); if (currentlySelectedTabIndex > -1) { viewPager.setCurrentItem(currentlySelectedTabIndex); } } } @Oviewride protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); if (viewPager != null) { int i = viewPager.getCurrentItem(); outState.putInt(FLAG_CURRENTLY_SELECTED_TAB_INDEX, i); } } } import android.support.v4.app.FragmentManager; import android.support.v4.view.ViewPager; import android.support.v7.app.ActionBair; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbair; public class MainActivity extends AppCompatActivity { private ViewPager viewPager; private static final String FLAG_CURRENTLY_SELECTED_TAB_INDEX = "currentlySelectedTabIndex"; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.mylayout); Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair actionBair = getSupportActionBair(); if (actionBair != null) { actionBair.setHomeAsUpIndicator(R.mipmap.ic_launcher); actionBair.setDisplayHomeAsUpEnabled(true); } viewPager = (ViewPager) findViewById(R.id.viewpager); FragmentManager fragmentManager = getSupportFragmentManager(); viewPager.setAdapter(getMyCustomAdapter()); TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); tabLayout.setupWithViewPager(viewPager); if (savedInstanceState != null) { final int currentlySelectedTabIndex = savedInstanceState.getInt(FLAG_CURRENTLY_SELECTED_TAB_INDEX, -1); if (currentlySelectedTabIndex > -1) { viewPager.setCurrentItem(currentlySelectedTabIndex); } } } @Oviewride protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); if (viewPager != null) { int i = viewPager.getCurrentItem(); outState.putInt(FLAG_CURRENTLY_SELECTED_TAB_INDEX, i); } } } import android.support.v4.app.FragmentManager; import android.support.v4.view.ViewPager; import android.support.v7.app.ActionBair; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbair; public class MainActivity extends AppCompatActivity { private ViewPager viewPager; private static final String FLAG_CURRENTLY_SELECTED_TAB_INDEX = "currentlySelectedTabIndex"; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.mylayout); Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair actionBair = getSupportActionBair(); if (actionBair != null) { actionBair.setHomeAsUpIndicator(R.mipmap.ic_launcher); actionBair.setDisplayHomeAsUpEnabled(true); } viewPager = (ViewPager) findViewById(R.id.viewpager); FragmentManager fragmentManager = getSupportFragmentManager(); viewPager.setAdapter(getMyCustomAdapter()); TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); tabLayout.setupWithViewPager(viewPager); if (savedInstanceState != null) { final int currentlySelectedTabIndex = savedInstanceState.getInt(FLAG_CURRENTLY_SELECTED_TAB_INDEX, -1); if (currentlySelectedTabIndex > -1) { viewPager.setCurrentItem(currentlySelectedTabIndex); } } } @Oviewride protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); if (viewPager != null) { int i = viewPager.getCurrentItem(); outState.putInt(FLAG_CURRENTLY_SELECTED_TAB_INDEX, i); } } } import android.support.v4.app.FragmentManager; import android.support.v4.view.ViewPager; import android.support.v7.app.ActionBair; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbair; public class MainActivity extends AppCompatActivity { private ViewPager viewPager; private static final String FLAG_CURRENTLY_SELECTED_TAB_INDEX = "currentlySelectedTabIndex"; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.mylayout); Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair actionBair = getSupportActionBair(); if (actionBair != null) { actionBair.setHomeAsUpIndicator(R.mipmap.ic_launcher); actionBair.setDisplayHomeAsUpEnabled(true); } viewPager = (ViewPager) findViewById(R.id.viewpager); FragmentManager fragmentManager = getSupportFragmentManager(); viewPager.setAdapter(getMyCustomAdapter()); TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); tabLayout.setupWithViewPager(viewPager); if (savedInstanceState != null) { final int currentlySelectedTabIndex = savedInstanceState.getInt(FLAG_CURRENTLY_SELECTED_TAB_INDEX, -1); if (currentlySelectedTabIndex > -1) { viewPager.setCurrentItem(currentlySelectedTabIndex); } } } @Oviewride protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); if (viewPager != null) { int i = viewPager.getCurrentItem(); outState.putInt(FLAG_CURRENTLY_SELECTED_TAB_INDEX, i); } } } import android.support.v4.app.FragmentManager; import android.support.v4.view.ViewPager; import android.support.v7.app.ActionBair; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbair; public class MainActivity extends AppCompatActivity { private ViewPager viewPager; private static final String FLAG_CURRENTLY_SELECTED_TAB_INDEX = "currentlySelectedTabIndex"; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.mylayout); Toolbair toolbair = (Toolbair) findViewById(R.id.toolbair); setSupportActionBair(toolbair); ActionBair actionBair = getSupportActionBair(); if (actionBair != null) { actionBair.setHomeAsUpIndicator(R.mipmap.ic_launcher); actionBair.setDisplayHomeAsUpEnabled(true); } viewPager = (ViewPager) findViewById(R.id.viewpager); FragmentManager fragmentManager = getSupportFragmentManager(); viewPager.setAdapter(getMyCustomAdapter()); TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); tabLayout.setupWithViewPager(viewPager); if (savedInstanceState != null) { final int currentlySelectedTabIndex = savedInstanceState.getInt(FLAG_CURRENTLY_SELECTED_TAB_INDEX, -1); if (currentlySelectedTabIndex > -1) { viewPager.setCurrentItem(currentlySelectedTabIndex); } } } @Oviewride protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); if (viewPager != null) { int i = viewPager.getCurrentItem(); outState.putInt(FLAG_CURRENTLY_SELECTED_TAB_INDEX, i); } } 

    Uma vez que getLastNonConfigurationInstance() foi obsoleto, em seu FragmentActivity simplesmente replace onRetainCustomNonConfigurationInstance () e usair o novo getLastCustomNonConfigurationInstance() :

     @Oviewride public Object onRetainCustomNonConfigurationInstance() { return mViewPager.getCurrentItem(); } 

    e recuperá-lo em seu onCreate() assim:

     Integer lastTab = (Integer) getLastCustomNonConfigurationInstance(); if (lastTab != null) { mViewPager.setCurrentItem(lastTab); } 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.