在片段之间发送意图

时间:2016-03-10 15:54:25

标签: java android android-intent

我最终需要将InteractionFragment中的信息发送到DisplayFragment。根据我的理解,这需要首先完成容器活动,因此我尝试从InteractionFragment发送到MainActivity。

public class MainActivity extends AppCompatActivity {
Fragment fragmentInteraction, fragmentHistory, fragmentDisplay;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FragmentManager fm = getSupportFragmentManager();
    fragmentInteraction = fm.findFragmentById(R.id.upper_container);

    if (fragmentInteraction == null) {
        fragmentInteraction = new InteractionFragment();
        fm.beginTransaction()
                .add(R.id.upper_container, fragmentInteraction)
                .commit();
    }

    fragmentDisplay = fm.findFragmentById(R.id.lower_container);

    if (fragmentDisplay == null) {
        fragmentDisplay = new DisplayFragment();
        fm.beginTransaction()
                .add(R.id.lower_container, fragmentDisplay)
                .commit();
    }

    public void onStart() {
    super.onStart();
    Intent intent = getIntent();
    String intentContents = intent.getStringExtra("editTextString");
    }
}


public class InteractionFragment extends Fragment implements View.OnClickListener {
Button buttonScramble, buttonHistory;
EditText mEditText;
Activity context;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        context = getActivity();
        View v = inflater.inflate(R.layout.fragment_interaction, parent, false);
        return v;
}

public void onStart() {
    super.onStart();
    buttonScramble = (Button) getActivity().findViewById(R.id.buttonScramble);
    buttonScramble.setOnClickListener(this);
    buttonHistory = (Button) getActivity().findViewById(R.id.buttonHistory);
    buttonHistory.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.buttonScramble:
            //set x to number of letters in word
            break;
        case R.id.buttonHistory:
            //call other fragment
            break;
        default:
            break;
    }
    //use this to send user input to display fragment
    //display fragment will use this info to shuffle letters
    mEditText  = (EditText) getActivity().findViewById(R.id.edit_text_display);
    Intent intent = new Intent(context, MainActivity.class);
    intent.putExtra("editTextString", mEditText.getText().toString());
    startActivity(intent);
}

intentContents返回null,我找不到其他类似问题的解决方案对我有用。任何建议将不胜感激!谢谢!

1 个答案:

答案 0 :(得分:0)

您是否尝试过使用OnFragmentInteractionListener?

这个答案:How to implement OnFragmentInteractionListener可能有帮助。