在活动/片段之间传递参数 [英] Passing parameters between activities/fragments

查看:88
本文介绍了在活动/片段之间传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,Android开发人员,

Hello Android developers,

我试图在两个不同的活动之间传递参数,但是我陷入了一个问题,无法摆脱.

I'm trying to pass arguments between two distinct activities but I've got stuck into an issue and I can't get out of it.

基本上,我正在尝试将两个字符串从MainActivity MainFragment 中包含的片段传递给辅助活动 SecondaryActivity

Basically, I'm trying to pass two strings from a fragment contained in my MainActivity, MainFragment to my secondary activity, SecondaryActivity

我可以将这两个字符串从 Other Activities 传递到 SecondaryActivity 而不会出现问题.我什至可以通过我的 NotificationService 来做到这一点,但是我不能通过该特定片段来实现.

I can pass those two strings from Other Activities to the SecondaryActivity without issues. I can even do that from my NotificationService, but I can't from that specific Fragment.

调试中,我可以看到我传递的第一个字符串已正确接收,但第二个则没有.还设置了变量,因此我没有尝试传递"null",而是在调试器上进行了检查.

Debugging, I can see that the first string I pass has been received correctly but the second one has been not. Variables are also set, so I'm not trying to pass something "null", I checked that on the debugger.

这是我的代码的一部分.

Here's part of my code.

MainFragment.java(发送方)

public class MainFragment extends Fragment {

public MainFragment() {
}

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

    final View view = inflater.inflate(R.layout.fragment_home,
            container, false);

    final TextView firstStringToPass = (TextView)view.findViewById(R.id.first);
    final TextView secondStringToPass = (TextView) view.findViewById(R.id.second);

    textView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final Intent secondaryActivityIntent = new Intent(v.getContext(), SecondaryActivity.class);
            Bundle args = new Bundle();
            args.putString(SecondaryActivity.ID_FIRST_STRING, firstStringToPass.getText().toString());
            args.putString(SecondaryActivity.ID_SECOND_STRING, secondStringToPass.getText().toString());
            secondaryActivityIntent.putExtras(args);
            getActivity().startActivity(secondaryActivityIntent, ActivityOptions.makeSceneTransitionAnimation(getActivity()).toBundle());
        }
    });
    return view;
}
}

SecondaryActivity.java(接收方)

public class SecondaryActivity extends AppCompatActivity {    
    public final static String PLAY_WORD = "com.etc.author.appname.STRING1";
    public final static String PLAY_IPA = "com.etc.author.appname.STRING2";
            ...
            @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.secondary_activity);

            Intent intent = getIntent();
            Bundle args = intent.getExtras();

            first_string = args.getString(ID_FIRST_STRING);
            second_string = args.getString(ID_SECOND_STRING);
        ....
        }
        ....
    }

任何帮助将不胜感激.

谢谢.

推荐答案

您的代码应该按照我所看到的工作,我注意到您将onClickListener设置为" new "语句.您确定这是您唯一一次在同一个TextView上执行此操作吗?

Your code should be working from what I see, I noticed you set the onClickListener doing a "new" statement. Are you sure this is the only time you do this on the same TextView?

这篇关于在活动/片段之间传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆