How to Intent into specific Tab Fragment?

╄→尐↘猪︶ㄣ 提交于 2020-01-01 06:30:06

问题


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;
        }

回答1:


  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~




回答2:


You can check it as

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

Happy Coding!




回答3:


viewpager.setCurrentItem(posotion);



回答4:


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);
}


来源:https://stackoverflow.com/questions/43178728/how-to-intent-into-specific-tab-fragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!