0

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.

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

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.

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 (Sender)

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 (Receiver)

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

Any help would be very very appreciated.

Thank you.

Cesarsk
  • 153
  • 1
  • 2
  • 14

1 Answers1

0

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?

Claff
  • 36
  • 7
  • Seems strange but yes, that was my issue: I had two listeners for my TextView: ListenerA (Put two parameters, like I wanted); ListenerB (Put one parameter, and that's why I couldn't be able to debug it in my code). So, distraction error, thanks for helping. – Cesarsk Mar 10 '17 at 13:25