java - 将字符串从 Activity 传递到 fragment

标签 java android android-fragments

我试图将一个字符串从 Activity 传递到 fragment ,以便稍后将其用作 fragment 中其他内容的键,但它返回空。我究竟做错了什么?这是我的 fragment 所在的 Activity 的代码

import androidx.appcompat.app.AppCompatActivity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class UserPage extends AppCompatActivity {

    String username;

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

        //this bit works fine, but later I also want to take string username and pass it to fragment
        username = getIntent().getExtras().getString("username");
        TextView welcome = findViewById(R.id.welcome_text);
        welcome.setText("Welcome, " + username);
    }

    public void flipTab(View v) {
        Fragment fragment = null;

        switch(v.getId()) {
            case R.id.btn_profile:
                fragment = new MyProfileFragment();
                break;
            case R.id.btn_edit:
                //another fragment here later
                break;
            case R.id.btn_colleges:
                fragment = new CollegeFragment();
                break;
        }
        //here I want to take the String username that came from the other activity and pass it to fragment
        Bundle args = new Bundle();
        args.putString("username", username);
        fragment.setArguments(args);

        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.fr, fragment);
        ft.commit();
    }
}

这是我遇到问题的 fragment

import android.content.Intent;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class MyProfileFragment extends Fragment {

    private String text;

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

        View myFragmentView = inflater.inflate(R.layout.fragment_my_profile, container, false);

        TextView example = myFragmentView.findViewById(R.id.ex);

        if(getArguments() != null) {
            text = getArguments().getString("username");
        } else {
            example.setText(text);
            System.out.println("String=" + text);
        }

        Button signOut = myFragmentView.findViewById(R.id.user_sign_out);
        signOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                System.out.println("Logged out");
                logOut();
            }
        });

        return myFragmentView;
    }

    public void logOut() {
        Intent intent = new Intent(getActivity(), MainActivity.class);
        startActivity(intent);
    }

}

我对 fragment 很陌生,如果我忽略了一些非常明显的东西,我很抱歉。但我尝试了一些东西,无论如何它们似乎都返回 null。任何帮助将不胜感激

最佳答案

有两种方法,要么使用Bundle,要么在fragment调用方法中传递参数

示例

按 bundle

Activity 中

Bundle bundle = new Bundle();
bundle.putString("edttext", "From Activity");
// set Fragmentclass Arguments
Fragmentclass fragobj = new Fragmentclass();
fragobj.setArguments(bundle);

以及在 fragment 任务中

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    String strtext = getArguments().getString("edttext");    
    return inflater.inflate(R.layout.fragment, container, false);
}

关于java - 将字符串从 Activity 传递到 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59121884/

相关文章:

java - 尝试做滑动窗口

android - ImageView 缩放到固定高度,但裁剪多余的宽度

java - 使用现有 App Engine 应用程序开发 Android 应用程序

java - 如何在每个 ActionBar 选项卡中使用不同的布局 (Android)

android - EventBus OnEvent() 没有被调用

java - 在使用 junit 进行测试期间,Android 处理程序为空

java - JasperReports java 库 : Can it handle PDF 2. 0 (ISO 32000-2 :2017)?

java - 解析 MappingNode 时主线程中出现异常

android - 使用 Activity 和 Fragment 处理推送通知导航

android - 保存图片 EXTRA_OUPUT 为空 onActivityResult