Search code examples
androidandroid-fragmentsandroid-tabbed-activity

How to pass a String from an activity to fragment of a tabbed activity in android


Before asking i have been through the similar questions and tried them but no success and every time it all ends with a null pointer exception. All i want to do is pass the username which is in string from one activity to another fragment of my tabbed activity. Here is what i tried, my main activity codes

Bundle bundle = new Bundle();
                bundle.putString("NAME", username);
                //PASS OVER THE BUNDLE TO OUR FRAGMENT
                ProfileFragment profileFragment = new ProfileFragment();
                profileFragment.setArguments(bundle);

                startActivity(new Intent(MainActivity.this, TabbedAct.class));

And codes from ProfileFragment

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.fragment_profile, container, false);

    profilename = (TextView) rootView.findViewById(R.id.profile);

    String name= this.getArguments().getString("NAME");

    profilename.setText(name);

    return rootView;
}

Solution

  • it is because ProfileFragment is not visible yet. You are starting an Activity not fragment.

    First, you need to intent the TabbedAct with extra value

    Intent intent = new Intent(MainActivity.this, TabbedAct.class);
    intent.putExtra("Name", username);
    startActivity(intent);
    

    Then, in TabbedAct Activity, you need to init your ProfileFragment

    String username = getIntent().getStringExtra("Name");
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    
    .....
    viewpager.setAdapter(viewPagerAdapter);
    viewpager.setCurrentItem(0);
    viewPagerAdapter.notifyDataSetChanged();
    

    And codes from ProfileFragment

    public static ProfileFragment newInstance(String username) {
        ProfileFragment f = new ProfileFragment();
        Bundle bdl = new Bundle();
        bdl.putString("NAME", username);
        f.setArguments(bdl);
        return f;
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_profile, container, false);
    
        profilename = (TextView) rootView.findViewById(R.id.profile);
    
        String name= this.getArguments().getString("NAME");
    
        profilename.setText(name);
    
        return rootView;
    }
    

    incase you don't have ViewPagerAdapter. You can use mine

    viewPagerAdapter.addFragment(,);
    viewPagerAdapter.addFragment(,"Tab2");
    viewPagerAdapter.addFragment(Fragment2.newsIntance(),"Tab3");
    
    public class ViewPagerAdapter extends FragmentPagerAdapter {
        private final String username;
    
        public ViewPagerAdapter(FragmentManager fragmentManager, String username) {
            super(fragmentManager);
            this.username = username;
        }
    
        @Override
        public Fragment getItem(int position) {
            if(position == 0) return ProfileFragment.newInstance(username);
            if(position == 1) return Fragment2.newsIntance();
            if(position == 2) return Fragment3.newsIntance();
    
            throw new IllegalStateException("Unexpected position " + position);
        }
    
        @Override
        public int getCount() {
            return 3;
        }
    
        @Override
        public CharSequence getPageTitle(int position) {
            if(position == 0) return "Tab1";
            if(position == 1) return "Tab2";
            if(position == 2) return "Tab3";
          
            throw new IllegalStateException("Unexpected position " + position);
        }
    }