繁体   English   中英

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

[英]Passing String from an activity to Fragment

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

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

这是我遇到问题的片段

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

}

我是碎片的新手,如果我忽略了一些非常明显的东西,我很抱歉。 但是我尝试了一些事情,无论如何它们似乎都返回 null。 任何帮助,将不胜感激

有两种方法,一种是使用 Bundle,另一种是在片段调用方法中传递参数

例子

按捆绑

活动中

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM