如何在恢复活动时显示默认选项卡?

时间:2016-06-21 09:14:55

标签: android

我有一个活动,其中我有Tablayout,并有两个标签为" Deal"," Story"当我导航到其他活动并再次恢复到该活动时 那个标签出现了我之前看过的,而我希望每当我恢复到该活动时总是显示默认标签。我该怎么做

代码: -

public void init() {
    s_oCloginSession = new CLoginSessionManagement(CMainActivity.this);// object creation of Login Session...
    setupToolbar();// setting toolbar

    // navigation bar code
    m_Drawer = (DrawerLayout) findViewById(R.id.drawer_layout);//finding id of drawerlayout
    s_drawerToggle = new ActionBarDrawerToggle(
            this, m_Drawer, m_Toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    m_Drawer.setDrawerListener(s_drawerToggle);
    m_Drawer.setScrimColor(getResources().getColor(android.R.color.transparent));
    s_drawerToggle.syncState();

    NavigationView m_Navigation = (NavigationView) findViewById(R.id.nav_view);
    m_Navigation.setNavigationItemSelectedListener(this);

    m_TabLayout = (TabLayout) findViewById(R.id.tab_layout);// finding Id of tablayout
    m_TabLayout.addTab(m_TabLayout.newTab().setText("Deals"));// add deal listin tab
    m_TabLayout.addTab(m_TabLayout.newTab().setText("Stories"));// add stories tab
    m_TabLayout.setTabGravity(TabLayout.GRAVITY_FILL);// setting Gravity of Tab

    m_ViewPager = (ViewPager) findViewById(R.id.pager);//finding Id of ViewPager
    CDealMainListingPager m_oDealMainScreenPager = new CDealMainListingPager
            (getSupportFragmentManager(), m_TabLayout.getTabCount());
    m_ViewPager.setAdapter(m_oDealMainScreenPager);// adiing adapter to ViewPager
    m_ViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(m_TabLayout));// performing action of page changing
    m_TabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            m_ViewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });

}

4 个答案:

答案 0 :(得分:2)

您可以创建一个名为BaseActivity的类,其扩展AppCompatActivity。现在,您的所有活动都将扩展到此BaseActivity课程,而不是AppCompatActivity。因此,无论何时未定义onBackPressed,它都会调用它的父类方法并执行它的操作。

public class BaseActivity extends AppCompatActivity  {

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public void onBackPressed() {
    super.onBackPressed();
    overridePendingTransition(R.anim.translate, R.anim.left_to_right_simple);
}

}
  

如果要为一个类自定义此方法,则只需要在该相应的类中重写此方法。对于其他类,它将与BaseActivity

中定义的相同      

你的所有活动都是这样的。

public class HomeScreen extends BaseActivity{
  @Override
   protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
   }

}

答案 1 :(得分:1)

上课,说CommonMethods并将方法放入该课程中:

public class CommonMethods extends AppCompatActivity {

    /*This method functions when user press device back button*/
    @Override
    public void onBackPressed() {
        long currentTime = System.currentTimeMillis();

        if ((currentTime - lastPressTime) < 2000) {
            // Double Press
            moveTaskToBack(true);
        } else {
            Toast.makeText(this, "Press again to exit", Toast.LENGTH_SHORT).show();
            lastPressTime = currentTime;
        }
    }
}

在这里,您可以根据自己的需要扩展ActivityAppCompatActivity

最后,您可以制作需要此功能的活动类,扩展此CommonMethods类。

答案 2 :(得分:1)

你可以通过一个公共类来实现这个功能,并在其中实现这个功能,

检查以下代码,

为方法

选择一个公共类
   //Here I am taking it as common class you can take it as per your choice

<强> CommonClass.java

  public class CommonClass
  {
      public bool manageBackPressed(Activity activity, long lastPressTime)
      {
           long currentTime = System.currentTimeMillis();

            if ((currentTime - lastPressTime) < 2000) {
                     return true;
             } else {
                  return false;
             }

            return false;
      }
  }

现在,在下面检查任何活动中的onBackPressed方法

   @Override
   public void onBackPressed() {

         if(commonClass.manageBackPressed(this, lastPressTime))
          {
               moveTaskToBack(true);
          }
          else
           {
                 Toast.makeText(this, "Press again to exit", Toast.LENGTH_SHORT).show();
                 lastPressTime = System.currentTimeMillis();
           }
    }

答案 3 :(得分:0)

我认为你以前的帖子也可以在后台使用,如果你不想更新,请取消它。