Dipak
Dipak

Reputation: 99

Intent From fragment to Second Tab

I have tab layout in android with viewpager in my Main Activity. I have three Tab with three Fragment. And in first fragment i have button i want to intent on second tab from that button on click. how to intent please help me. Here is my Code

public class MainActivity extends AppCompatActivity {

    //Declaring All The Variables Needed

    private Toolbar toolbar;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private ViewPagerAdapter viewPagerAdapter;

    public static final String PREFS_NAME = "MyPrefsFile";


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


        SharedPreferences settings = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        String name = settings.getString("first", "str1");

        //str1 = intent.getStringExtra("first");
        Toast.makeText(MainActivity.this, "" + name, Toast.LENGTH_SHORT).show();


        toolbar = (Toolbar) findViewById(R.id.toolbar);
        tabLayout = (TabLayout) findViewById(R.id.tabs);
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        FragmentManager manager = getSupportFragmentManager();
        ViewPagerAdapter adapter = new ViewPagerAdapter(manager);
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.setTabsFromPagerAdapter(adapter);

        tabLayout.setTabTextColors(ContextCompat.getColorStateList(this, R.drawable.tab_selector));
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        setSupportActionBar(toolbar);
        final FloatingActionMenu floatingActionsMenu=(FloatingActionMenu) findViewById(R.id.material_design_android_floating_action_menu);
        final FloatingActionButton floatingActionButton1=(FloatingActionButton)findViewById(R.id.material_design_floating_action_menu_item1);
        floatingActionButton1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "Table", Toast.LENGTH_SHORT).show();
                viewPager.setCurrentItem(0);
              floatingActionsMenu.close(true);
            }
        });

        FloatingActionButton floatingActionButton2=(FloatingActionButton)findViewById(R.id.material_design_floating_action_menu_item2);
        floatingActionButton2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "Order", Toast.LENGTH_SHORT).show();
                viewPager.setCurrentItem(1);
                floatingActionsMenu.close(true);
            }
        });
        FloatingActionButton floatingActionButton3=(FloatingActionButton)findViewById(R.id.material_design_floating_action_menu_item3);
        floatingActionButton3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "Logout", Toast.LENGTH_SHORT).show();
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            Intent tologin=new Intent(MainActivity.this,LoginPage.class);
            startActivity(tologin);
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

}

Upvotes: 0

Views: 1244

Answers (2)

rushi
rushi

Reputation: 277

Recommended approach : you can implement a listener in second fragment and create reference in first fragment. after onclick of a button from 1st fragment call method of listener which is defined in second fragment.

Second approach : you can maintain a static data from 1st fragment and on click just load 2nd fragment and access that static data.

Upvotes: 0

Vishal Chauhan
Vishal Chauhan

Reputation: 917

All the fragments are within the same ViewPager and you just want to go one to another fragment you can do like this...

The view pager is in Activity but the Button is inside Fragment so you need to do this create this method inside your activity

public void navigateFragment(int position){
viewPager.setCurrentItem(position, true); 

}

and call inside your OnClickListener method of fragment:

((MainActivity)getActivity()).navigateFragment(POSITION_YOU_WANNA_SELECT);

Hope this will help you.

Upvotes: 1

Related Questions