Search code examples
androidandroid-fragmentsandroid-tablayout

How to Intent into specific Tab Fragment?


T have two Activities: MainActivity and LoginActivity. I have one Fragment too, the name is TabFragment (with a TabLayout), its parent is MainActivity. How can I intent into the specific TabFragment from my Login Activity.

This is my Main Activity:

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        id = getIntent().getStringExtra("id");


        if (savedInstanceState == null) {
     getSupportFragmentManager().beginTransaction().replace(R.id.confrag, new FragmentUtama()).commit();
            getSupportFragmentManager().beginTransaction().replace(R.id.confrag, new TabFragment()).commit();
        }
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        sharedPreferences = getSharedPreferences("login.conf", MODE_PRIVATE);
        editor = sharedPreferences.edit();
        String username = sharedPreferences.getString("username", "");
        String password = sharedPreferences.getString("password", "");
        Log.d(TAG, "Username:"+username+"\npassword:"+password);


        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

    }

And This my Tab Fragment:

  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.viewpager, null);

        tabLayout = (TabLayout)view.findViewById(R.id.tabs);
        viewPager = (ViewPager)view.findViewById(R.id.viewpager);
        viewPager.setAdapter(new Adapterbaru(getChildFragmentManager()));
         tabLayout.post(new Runnable() {
            @Override
            public void run() {
                tabLayout.setupWithViewPager(viewPager);
            }
        });
        Long tsLong = System.currentTimeMillis()/1000;
        String ts = tsLong.toString();


        return view;
    }

    public interface OnFragmentInteractionListener {

        void onFragmentInteraction(Uri uri);
    }
    public class Adapterbaru extends FragmentPagerAdapter{
        public Adapterbaru(FragmentManager fm){
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position){
                case 0 : {
                    return new Fragmentone();
                }
                case 1 : {
                    return new FragmentTwo();

                }
                case 2 : {
                    return new Fragmenttree();

                }
            }
            return null;
        }

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

        @Override
        public CharSequence getPageTitle(int position) {
            switch(position){
                case 0 : {
                    return "First";

                }
                case 1 : {
                    return "Second";

                }
                case 2 : {
                    return "Third";
                }
            }
            return null;
        }

Solution

    1. From your LoginActivity, pass tabs fragment (FragmentOne, FragmentTwo, FragmentThree) id as 0, 1, 2 through Intent.

      Intent intent  = new Intent(LoginActivity.this, MainActivity.class);
      intent.putExtra("FRAGMENT_ID", 0);
      startActivity(intent);
      
    2. In your MainActivity, get the fragment id from Intent

      // Inside OnCreate()
      int fragmentId = getIntent().getIntExtra("FRAGMENT_ID", 0);
      
    3. From MainActivity, show TabFragment using FragmentManager and pass fragmentId to TabFragment using Bundle

      // Inside OnCreate()
      Bundle bundle = new Bundle();
      bundle.putString("TARGET_FRAGMENT_ID", fragmentId);
      TabFragment tabFragment = new TabFragment();
      tabFragment.setArguments(bundle);
      
      getSupportFragmentManager().beginTransaction().replace(R.id.confrag, tabFragment).commit();
      
    4. In TabFragment, get target fragment (FragmentOne, FragmentTwo, FragmentThree) id from Bundle and finally show desired fragment using ViewPager.setCurrentItem(POSITION) method.

      // Inside OnCreateView()
      int position = getArguments().getInt("TARGET_FRAGMENT_ID"); 
      viewpager.setCurrentItem(position);
      

    Hope this will help~