Switching to first tab in tabbed activity not working properly in android

75
January 13, 2018, at 09:22 AM

So I have a weird problem. Basically I have a Main activity with four buttons, each button opens a different tab (there is 4 tabs). Now If I click 4th button I go to the 4th fragment but then if I try clicking on the first tab in tabbed activity it doesn't do anything.

So I can't go to the first tab if I start from 2nd 3rd or 4th unless I click on any other tab first.

It's only the first tab that works like this which makes me think that maybe there's some value that should be changing on menu button click but doesn't?

Here's my TabsActivity onCreate:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tabs);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    //
    int defaultValue = 5;
    //int page = getIntent().getIntExtra("One", defaultValue);
    mViewPager.setCurrentItem(getIntent().getIntExtra("One", defaultValue));
    //
    //
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    //
    mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager))

Also here's TabsActivity class that takes care of selecting fragments:

public class SectionsPagerAdapter extends FragmentPagerAdapter {
    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }
    @Override
    public Fragment getItem(int position) {
        switch (position){
            case 0:
                tab1recipes tab1 = new tab1recipes();
                return tab1;
            case 1:
                tab2timer tab2 = new tab2timer();
                return tab2;
            case 2:
                tab3measure tab3 = new tab3measure();
                return tab3;
            case 3:
                tab4substit tab4 = new tab4substit();
                return tab4;
            default:
                return null;
        }
    }

Here's MainActivity:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button btn1 = (Button) findViewById(R.id.recipesbtn);
    Button btn2 = (Button) findViewById(R.id.timerbtn);
    Button btn3 = (Button) findViewById(R.id.measurebtn);
    Button btn4 = (Button) findViewById(R.id.subsbtn);
    btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            int page=0;
            Intent intent = new Intent(MainActivity.this, TabsActivity.class);//startActivity();
            intent.putExtra("One", page);
            startActivity(intent);
        }
    });
    btn2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            int page=1;
            Intent intent = new Intent(MainActivity.this, TabsActivity.class);//startActivity();
            intent.putExtra("One", page);
            startActivity(intent);
        }
    });
    btn3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            int page=2;
            Intent intent = new Intent(MainActivity.this, TabsActivity.class);//startActivity();
            intent.putExtra("One", page);
            startActivity(intent);
        }
    });
    btn4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            int page=3;
            Intent intent = new Intent(MainActivity.this, TabsActivity.class);//startActivity();
            intent.putExtra("One", page);
            startActivity(intent);
        }
    });
READ ALSO
How do we show a notification in the OS with React Native and Android?

How do we show a notification in the OS with React Native and Android?

So I just set up a Firebase function to send notifications to my devices and I added react-native-firebase to my project so I signin, get a token and receive notifications

81
Android 8.0 Oreo RunningServiceInfo returns 0 services

Android 8.0 Oreo RunningServiceInfo returns 0 services

my Oneplus 5 just updated to Oreo and something has brokenI have a simple widget which starts and stops some service

75
In App purchase in Libgdx [on hold]

In App purchase in Libgdx [on hold]

I am planing to integrate in-app purchase in my Libgdx game but unfortunately Libgdx does not have a good (in-app purchasing) implementation tutorial on their Github wiki and the only source is to look into https://githubcom/libgdx/gdx-pay so I copy-pasted...

77
Using fragments with NativeActivity

Using fragments with NativeActivity

I have a fully working C++/OpenGL engine that I ported over to Android using their supplied NativeActivityBut I'm now tasked with integrating it to an older project which was 100% Java, with plenty of Fragments

91