How can a load to a specific tab / fragment in a Tablayout from a different activity using the Intent?

2.8k views Asked by At

In activity A is a button. When you click the button in activity A it'll take you to TabbedActivity which has 5 tabs / fragments that show a list. Everytime I click on the button it will load to Tab1 by default. How can I make it load into Tab2 through the Onclick method? Grateful for any help. Try to leave detailed answers please, I'm new and get lost easily lol.

The onClick method in Activity A:

  public void buttonInActivityA(View view) {

    Intent intent = new Intent(getBaseContext(), TabbedActivity.class);
    intent.putExtra("EXTRA_PAGE", 2);
    startActivity(intent);

}

}

TabbedActivity which is a TabbedLayout:

public class TabbedActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tabbed_activity);

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        String value = extras.getString("EXTRA_PAGE");
        int position= Integer.parseInt(value );
        ViewPager viewPager;
        viewPager = (ViewPager) findViewById(R.id.pager);
        viewPager.setCurrentItem(2);
    }


        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        TextView toolTitle = (TextView) findViewById(R.id.toolbar_title);
        toolTitle.setText(R.string.app_name);
        toolTitle.setTextColor(Color.parseColor("#FFFFFF"));
        setSupportActionBar(toolbar);

        // add back arrow to toolbar_layout
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowHomeEnabled(true);
        }

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
        tabLayout.addTab(tabLayout.newTab().setText("PC HEALTH"));
        tabLayout.addTab(tabLayout.newTab().setText("WIFI"));
        tabLayout.addTab(tabLayout.newTab().setText("MISC"));
        tabLayout.addTab(tabLayout.newTab().setText("HOW TO"));
        tabLayout.addTab(tabLayout.newTab().setText("SHORT CUTS"));
        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));
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {

            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

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

            }

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

            }
        });
    }

This is the page adapter for the fragments:

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:
            FragmentOne tab1 = new FragmentOne();
            return tab1;
        case 1:
            FragmentTwo tab2 = new FragmentTwo();
            return tab2;
        case 2:
            FragmentThree tab3 = new FragmentThree();
            return tab3;
        default:
            return null;
    }
}

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

}

FragmentTwo the frag and tab I need to load when clicked:

public class FragmentTwo extends Fragment {


public FragmentWifi() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {


    return inflater.inflate(R.layout.tab_fragment_2, container, false);


}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    // THIS IS HOW TO ADD LISTVIEWS INTO A FRAGMENT, FUCK YEAH!

    ListView lv = (ListView)getActivity().findViewById(R.id.listView2);


    String[] values = new String[] { "Example", "Example",
            "Example", "Example" };
    ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(),
            android.R.layout.simple_list_item_1, values);

    lv.setAdapter(adapter);



    // Handles items being clicked
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            if (position == 0) {
                Intent intent = new Intent(getContext(), RandomActivity.class);
                startActivity(intent);
            }
        }
    }
    );

}

}

1

There are 1 answers

0
Moien.Dev On

First of all you can change default tab for TabActivity.

// set the default tab to the second tab   
      viewPager.setCurrentItem(1, false);

But it would be better to pass data to tabactivity and change it dynamically .

public class TheActivity extends TabActivity {
    public static TheActivity self;
    ...
    @Override
    public void onCreate(Bundle savedInstanceState) {
        self=this;

on any Activity running in a tab, when you want to change the one shown on your app. you can do this:

TabHost tabHost = TheActivity.self.getTabHost();
tabHost.setCurrentTab(0);