0

My problem is passing string from activity to fragment. I researched for a whole day and my problem isn't solved.

activity :

if(email.matches(users.user1)&&password.matches(users.pass1)){

        Intent intent = new Intent(LoginActivity.this,MainActivity.class);

        Bundle bundle = new Bundle();
        bundle.putString("user", users.name);
        ProfileFragment fragobj = new ProfileFragment();
        fragobj.setArguments(bundle);


        startActivity(intent);

    }

fragment :

public class ProfileFragment extends Fragment {

public static ProfileFragment newInstance() {
    return new ProfileFragment();


}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.fragment_profile, container, false);

    String user = getArguments().getString("user");
    TextView textView = v.findViewById(R.id.namef1);
    textView.setText(user);


    return inflater.inflate(R.layout.fragment_profile, container, false);

}


}

MainActivity

public class MainActivity extends Activity {



private BottomNavigationView mBottomNavigationView;

private Toolbar supportActionBar;


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

    setupBottomNavigation();

    if (savedInstanceState == null) {

        loadHomeFragment();
    }


}


private void setupBottomNavigation() {

    mBottomNavigationView =findViewById(R.id.bottom_navigation);

    mBottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {

            switch (item.getItemId()) {
                case R.id.action_home:
                    loadHomeFragment();
                    return true;
                case R.id.action_profile:
                    loadProfileFragment();
                    return true;
                case R.id.action_settings:
                    loadSettingsFragment();
                    return true;
            }
            return false;
        }
    });
}


private void loadHomeFragment() {

    HomeFragment fragment = HomeFragment.newInstance();
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.replace(R.id.fragment_frame, fragment);
    ft.commit();
}


private void loadProfileFragment() {

    ProfileFragment fragment = ProfileFragment.newInstance();
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.replace(R.id.fragment_frame, fragment);
    ft.commit();
}

private void loadSettingsFragment() {

    SettingsFragment fragment = SettingsFragment.newInstance();
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.replace(R.id.fragment_frame, fragment);
    ft.commit();
}

public void setSupportActionBar(Toolbar supportActionBar) {
    this.supportActionBar=supportActionBar;
}
}

as it shown in code user has login and one of his data display in a profile fragment . I wanna display this string as Textview in profilefragment . anybody have an idea?

ALI
  • 21
  • 7

1 Answers1

0

Move below part of code to the place where you are adding the ProfileFragment to Activity.

Bundle bundle = new Bundle();
    bundle.putString("user", users.name);
    ProfileFragment fragobj = new ProfileFragment();
    fragobj.setArguments(bundle);
Sanjeet
  • 2,385
  • 1
  • 13
  • 22