I will ultimately need to send information from InteractionFragment to DisplayFragment. To my understanding, this needs to go through the container activity first, so I'm trying to send from InteractionFragment to 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 returns null and no other solutions I've found for similar problems are working for me. Any suggestions would be greatly appreciated! Thanks!