Passing parameters between activities/fragments

2019-09-08 17:53发布

问题:

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.

回答1:

I will Explain all scenarios

When You pass data From Activity To a fragment You can use setArguments that takes a Bundle contain your data Function on Fragment Instance exactly before committing the Transition

An Example

 DetailsFragment dFragment = new DetailsFragment();
                    Bundle bundle = new Bundle();
                    bundle.putParcelable(Intent.EXTRA_TEXT, movies);
                    dFragment.setArguments(bundle);
                    getSupportFragmentManager().beginTransaction().replace(R.id.panel_two_id, dFragment).commit(); 

the Second scenario is

In your fragment you can call getActivity(). This will give you access to the activity that created the fragment. From there you can obviously call any sort of accessor methods that are in the activity.



回答2:

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?