3

First of all I am new to Android development, and I would appreciate your help.

I have a tabbed activity in android studio and I have a FloatingActionButton to add an Item in each fragment of the tabbed activity. This button then calls a new Activty and I would like to pass to the new activity the tab Number, so that I can store it in a database.

How can I get the tab Number that the new activity was generated from?

Below is the code:

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


      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
      setSupportActionBar(toolbar);

      mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());


      mViewPager = (ViewPager) findViewById(R.id.container);
      mViewPager.setAdapter(mSectionsPagerAdapter);

      TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);

      mViewPager.addOnPageChangeListener(new 
     TabLayout.TabLayoutOnPageChangeListener(tabLayout));
     tabLayout.addOnTabSelectedListener(new 
     TabLayout.ViewPagerOnTabSelectedListener(mViewPager));


      FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
      fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Intent startNewPostActivity = new Intent(getApplicationContext(), NewPostActivity.class);
            startNewPostActivity.putExtra("Fragment_Position", position_fragment);   

            startActivity(startNewPostActivity);
        }
    });



}

To get the position I was doing the following : Setting position_fragment = position; however this returns wrong tab Number.

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class below).
         position_fragment = position;  // not good
        return PlaceholderFragment.newInstance(position);
    }

    @Override
    public int getCount() {
        // Show 5 total pages.
        return 5;
    }
}

}

Kurt Camilleri
  • 133
  • 1
  • 1
  • 11

3 Answers3

2

Try this:

fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Intent startNewPostActivity = new Intent(getApplicationContext(), NewPostActivity.class);
            startNewPostActivity.putExtra("Fragment_Position", mViewPager.getCurrentItem());   

            startActivity(startNewPostActivity);
        }
    });
Levi Moreira
  • 11,917
  • 4
  • 32
  • 46
2

You can identify your tab id from these switch case and call necessary function inside

tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());//setting current selected item over viewpager
                switch (tab.getPosition()) {
                    case 0:
                        //Log.e("TAG","TAB1");
                        break;
                    case 1:
                        //Log.e("TAG","TAB2");
                        break;
                    case 2:
                        //Log.e("TAG","TAB3");
                        break;
                    case 3:
                        //Log.e("TAG","TAB4");
                        break;
                    case 4:
                        //Log.e("TAG","TAB5");
                        break;
                    case 5:
                        //Log.e("TAG","TAB6");
                        break;
                    case 6:
                        //Log.e("TAG","TAB7");
                        break;
                }
            }

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

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
Zobair Alam
  • 527
  • 1
  • 5
  • 24
1

There are two ways to get number of the Current Tab in Tabbed Activity

1. use ViewPager.getCurrentItem()

Returns the number of pages that will be retained to either side of the current page in the view hierarchy in an idle state. Defaults to 1.

SAMPLE CODE

fab.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        Intent startNewPostActivity = new Intent(getApplicationContext(), NewPostActivity.class);
        startNewPostActivity.putExtra("Fragment_Position", mViewPager.getCurrentItem());   

        startActivity(startNewPostActivity);
    }

2. Use TabLayout.addOnTabSelectedListener()

Add a TabLayout.OnTabSelectedListener that will be invoked when tab selection changes. SAMPLE CODE

    tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            int position = tab.getPosition();
        }

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

        }

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

        }
    });
AskNilesh
  • 67,701
  • 16
  • 123
  • 163