How to Intent into specific Tab Fragment?

人盡茶涼 提交于 2019-12-03 20:19:11
  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~

You can check it as

if(isFromLogin){
  viewpager.setCurrentItem(specificFragmentPosition);
}

Happy Coding!

viewpager.setCurrentItem(posotion);

From your LoginActivity pass Intents like this

In LoginActivity.class

Intent intent  = new Intent(LoginActivity.this, MainActivity.class);
intent.putExtra("Fragmentone", 0); //pass zero for Fragmentone.
startActivity(intent);

Intent intent  = new Intent(LoginActivity.this, MainActivity.class);
    intent.putExtra("FragmentTwo", 1); //pass zero for FragmentTwo.
    startActivity(intent);

Intent intent  = new Intent(LoginActivity.this, MainActivity.class);
    intent.putExtra("FragmentThree", 2); //pass zero for FragmentThree.
    startActivity(intent);

In MainActivity.class

int value = getIntent().getIntExtra("Fragmentone", 0);

From Your activity pass like this

Bundle bundle = new Bundle();
bundle.putString("key", value);
FragmentOne fragmentOne = new FragmentOne();
fragmentOne.setArguments(bundle);

and inside oncreateView of Fragment please define this

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    int strtext = getArguments().getInt("key");    
    return inflater.inflate(R.layout.fragment, container, false);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!