Ive got two Fragments. One for data Input and one to display them in a ListView. But I dont know how to send data from Fragment 1 to Fragment 2...
I know that I can get Data from my MainActivity to Fragment 1 with the following Code:
String listElement = getActivity().getIntent().getStringExtra("com.sample.MESSAGE");
But how do I send the string from Fragment 2 to my Activity?
UPDATE: I'm not quite sure if I'm doing this right... I used Harlo Holmes Tutorial (posted below) and not Ive got this Fragment Code
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Button;
public class AddDataFragment extends Fragment {
public interface OnDataPass {
public void onDataPass(String data);
}
Button buttonadd;
Button buttondelete;
private EditText inputProduct;
private TextView listElement;
OnDataPass dataPasser;
@Override
public void onAttach(Context context){
super.onAttach(context);
Activity a;
if (context instanceof Activity){
a=(Activity) context;
}
dataPasser = (OnDataPass) context;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_add_data, container, false);
inputProduct = (EditText) view.findViewById(R.id.editText_product);
buttonadd = (Button) view.findViewById(R.id.button_add_addData);
buttondelete = (Button) view.findViewById(R.id.button_delete_addData);
listElement = (TextView) view.findViewById(R.id.productTest);
buttonadd.setEnabled(true);
buttondelete.setEnabled(true);
buttonadd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listElement.setText(getString(R.string.ListElementPlaceholder, inputProduct.getText()));
}
public void passData(String listElement){
dataPasser.onDataPass(listElement);
}
});
return view;
}
}
There where some issues with the onAttach method... Is this the right way?