0

I have created tablayout in my app as follow:

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_place_info);
        getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        getSupportActionBar().setCustomView(R.layout.action_bar);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
        tabLayout.addTab(tabLayout.newTab().setText("الموقع"));
        tabLayout.addTab(tabLayout.newTab().setText("تفاصيل التصنيف"));
        tabLayout.addTab(tabLayout.newTab().setText("ساعات العمل"));
        tabLayout.addTab(tabLayout.newTab().setText("المتطلبات"));
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

        final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
        final PagerAdapter adapter = new PagerAdapter
                (getSupportFragmentManager(), tabLayout.getTabCount());
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        viewPager.setCurrentItem(4);
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());

                int position = tab.getPosition();
                switch(position){
                    case 0:
                        break;
                    case 1:
                        break;
                    case 2:
                        break;
                    case 3:
                        break;
                }

            }

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

            }

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

            }
        });
    }

The Page adapter is:

public class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;

public PagerAdapter(FragmentManager fm, int NumOfTabs) {
    super(fm);
    this.mNumOfTabs = NumOfTabs;
}

@Override
public Fragment getItem(int position) {

    switch (position) {
        case 0:
            LocationInfoFragment tab1 = new LocationInfoFragment();
            return tab1;
        case 1:
            RankInfoFragment tab2 = new RankInfoFragment();
            return tab2;
        case 2:
            WorkingHoursInfoFragment tab3 = new WorkingHoursInfoFragment();
            return tab3;
        case 3:
            WorkingHoursInfoFragment tab4 = new WorkingHoursInfoFragment();
            return tab4;
        default:
            return null;
    }
}

@Override
public int getCount() {
    return mNumOfTabs;
}
}

I know that when I want to start an activity I have to use Intent as follow:

Intent processesListAcitivity = new Intent(getApplicationContext(), ProcessesListAcitivity.class);
            startActivity(processesListAcitivity);

My question is where to put it:

in the switch in the first class, or in the page adapter ?!

McLan
  • 2,552
  • 9
  • 51
  • 85
  • 3
    Most likely, the answer is "neither". You start an activity when the user clicks on something that indicates it is time to start the activity, like a `Button` or action bar item or nav drawer entry. You do not start an activity because the user switched to some other tab, because then *the user cannot work with the contents of that tab*. – CommonsWare Jan 28 '17 at 19:23
  • @CommonsWare : fair enough, but how to load my layout for that tab ? for example, if I needed to show a ListView ?! I want to be able to do that with tab change – McLan Jan 28 '17 at 19:26
  • 1
    When do you want to open your activity? When the user switches the tab? If so, you're doing it wrong. – Anis LOUNIS aka AnixPasBesoin Jan 28 '17 at 19:27
  • @AnixPasBesoin : Yes, when user switch between tabs .. in case I am doing it wrong, how to do it right then. thanks – McLan Jan 28 '17 at 19:28
  • 1
    You can implement a [ViewPager.OnPageChangeListener](https://developer.android.com/reference/android/support/v4/view/ViewPager.OnPageChangeListener.html) and start youractivity in the onPageSelected callback. – Anis LOUNIS aka AnixPasBesoin Jan 28 '17 at 19:32
  • [http://stackoverflow.com/questions/30539772/android-tablayout-android-design](http://stackoverflow.com/questions/30539772/android-tablayout-android-design) – Moien.Dev Jan 28 '17 at 19:33
  • "but how to load my layout for that tab ? " -- that is the job of the fragment that you are showing in that tab. – CommonsWare Jan 28 '17 at 19:38
  • @AnixPasBesoin : I don't have `onPageChnageListener` method to call. Beside, in the above code I am using `onAddOnPageChangeListener` which then has `onTabSelected` where I am implementing the `switch`. So, you are suggesting that I should start my activity in this `switch`, right ?! but again, what is the need for the PageAdaptor's switch that start the fragment ? – McLan Jan 28 '17 at 19:52
  • 1
    It's not a method it's an interface. yourPager.setOnPageChangeListener( new ... ) see the link I sent you. – Anis LOUNIS aka AnixPasBesoin Jan 28 '17 at 19:54
  • @AnixPasBesoin: please could you clarify for me my confusion. If I using `onPageSelected` with the `addOnPageChangeListener`, then why do I need the `PageAdapter` that inflate fragments ?! – McLan Jan 28 '17 at 20:08
  • @CommonsWare: please could you clarify for me my confusion. If I using the fragment to load my layout, then why do I need the listeners in my activity ? – McLan Jan 28 '17 at 20:19
  • You have two listeners there. `addOnPageChangeListener()` is to tie the `ViewPager` to the `TabLayout`. `addOnTabSelectedListener()` is not necessary, as you are not doing anything with it. In [this sample app](https://github.com/commonsguy/cw-omnibus/tree/master/DesignSupport/TabLayout), I use `TabLayout` with a `ViewPager` with neither of those listeners. – CommonsWare Jan 28 '17 at 20:34

1 Answers1

0
private Stack<String> stack;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (stack == null)
    stack = new Stack<String>();
// start default activity
push("FirstStackActivity", new Intent(this, Tab_SampleActivity.class));
}

@Override
public void finishFromChild(Activity child) {
pop();
}

@Override
public void onBackPressed() {
pop();

}

public void push(String id, Intent intent) {
Window window = getLocalActivityManager().startActivity(id,               intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
 if (window != null) {
    stack.push(id);
    setContentView(window.getDecorView());
}
}

public void pop() {
if (stack.size() == 1)
    finish();
LocalActivityManager manager = getLocalActivityManager();
manager.destroyActivity(stack.pop(), true);
if (stack.size() > 0) {
    Intent lastIntent = manager.getActivity(stack.peek()).getIntent();
    Window newWindow = manager.startActivity(stack.peek(), lastIntent);
    setContentView(newWindow.getDecorView());
}

}

Divyesh Rudani
  • 231
  • 1
  • 2
  • 13