将字符串从活动传递到片段

时间:2017-12-08 14:32:20

标签: android fragment parameter-passing

我的问题是将字符串从活动传递到片段。我研究了一整天,我的问题没有解决。

活动:

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

    }

片段:

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

如代码中所示,用户具有登录功能,其中一个数据显示在配置文件片段中。我想在profilefragment中将此字符串显示为Textview。 有谁有想法?

1 个答案:

答案 0 :(得分:0)

将部分代码移到您将ProfileFragment添加到Activity的位置。

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