2

In the below code I am tryin to pass sensor values from Frag1 to Frag2 through the mainActivity. Frag1 implements sensorEvenListener and in Frag1's onResume i pass the values to the mainActivtiy by sendValues.onSendValues(getAccX(), getAccY(), getAccZ()); the sendValues is an object of the interface called onSendListener which is implemented in the mainActivity.

In the MainActivity I pass the data to Frag2 using

@Override
public void onSendValues(float x, float y, float z) {
    // TODO Auto-generated method stub
    Bundle b = new Bundle();
    b.putFloat("x", x);
    b.putFloat("y", y);
    b.putFloat("z", z);

    Frag2 frag2 = new Frag2();
    frag2.setArguments(b);
}

as shown below in the code.

the problem is, when i check if the bundle sent to Frag2 is null or not, i find it is null, why it is null?

please find the code posted below

** MainActivity**:

public class MainActivity extends Activity implements onSendListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);
}

/**
 * to send values from frag1 to frag2 through mainActivity. use bundle.
 */
@Override
public void onSendValues(float x, float y, float z) {
    // TODO Auto-generated method stub
    Bundle b = new Bundle();
    b.putFloat("x", x);
    b.putFloat("y", y);
    b.putFloat("z", z);

    Frag2 frag2 = new Frag2();
    frag2.setArguments(b);
}
}

**Frag1 **:

public class Frag1 extends Fragment implements SensorEventListener {

private SensorManager sensorManager;
TextView tvAccX;
TextView tvAccY;
TextView tvAccZ;

private float x = 0.0f;
private float y = 0.0f;
private float z = 0.0f;

private void setAccX(float x) {
    this.x = x;
}
private float getAccX() {
    return this.x;
}

private void setAccY(float y) {
    this.y = y;
}
private float getAccY() {
    return this.y;
}

private void setAccZ(float z) {
    this.z = z;
}
private float getAccZ() {
    return this.z;
}

Button btnSend;
onSendListener sendValues;

@Override
public void onAttach(Activity activity) {
    // TODO Auto-generated method stub
    super.onAttach(activity);

    sendValues = (onSendListener) activity;
    sensorManager = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View view = inflater.inflate(R.layout.frag_1, container, false);

    tvAccX = (TextView) view.findViewById(R.id.tv_accX_value);
    tvAccY = (TextView) view.findViewById(R.id.tv_accY_value);
    tvAccZ = (TextView) view.findViewById(R.id.tv_accZ_value);
    btnSend = (Button) view.findViewById(R.id.btn_send);

    return view;
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onViewCreated(view, savedInstanceState);


}

@Override
public void onSensorChanged(SensorEvent event) {
    // TODO Auto-generated method stub
    switch (event.sensor.getType()) {
    case Sensor.TYPE_ACCELEROMETER:
        showAccReadings(event);
        break;

    default:
        break;
    }
}

private void showAccReadings(SensorEvent event) {
    // TODO Auto-generated method stub
    float[] values = event.values;

    float x = values[0];
    float y = values[1];
    float z = values[2];

    setAccX(x);
    setAccY(y);
    setAccZ(z);

    tvAccX.setText(String.valueOf(x));
    tvAccY.setText(String.valueOf(y));
    tvAccZ.setText(String.valueOf(z));
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
    // TODO Auto-generated method stub

}

@Override
public void onResume() {
    // TODO Auto-generated method stub
    super.onResume();

    Sensor accSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    sensorManager.registerListener(this, accSensor, sensorManager.SENSOR_DELAY_FASTEST);

    btnSend.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            sendValues.onSendValues(getAccX(), getAccY(), getAccZ());
        }
    });
}

@Override
public void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    sensorManager.unregisterListener(this);
}

}

**Frag2 **:

public class Frag2 extends Fragment {

float x;
float y;
float z;

TextView tvX;
TextView tvY;
TextView tvZ;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub

    x = getArguments().getFloat("x");
    y = getArguments().getFloat("y");
    z = getArguments().getFloat("z");

    View view = inflater.inflate(R.layout.frag_2, container, false);

    tvX = (TextView) view.findViewById(R.id.tv_accX2_label);
    tvY = (TextView) view.findViewById(R.id.tv_accY2_label);
    tvZ = (TextView) view.findViewById(R.id.tv_accZ2_label);

    return view;
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onViewCreated(view, savedInstanceState);

    if (getArguments() != null) {
        Log.d("Frag2", "getArgument() is not NULL");
        x = getArguments().getFloat("x");
        y = getArguments().getFloat("y");
        z = getArguments().getFloat("z");
    } else {
        Log.d("Frag2", "getArgument() is NULL");
    }

    tvX.setText(String.valueOf(x));
    tvY.setText(String.valueOf(y));
    tvZ.setText(String.valueOf(z));
}
}

** mainActivity.xml**:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}">

<fragment
    android:name="com.example.fragmentcommunication_00.Frag1"
    android:id="@+id/frag_1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"/>

<fragment
    android:name="com.example.fragmentcommunication_00.Frag2"
    android:id="@+id/frag_2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="50dp"
    android:layout_centerHorizontal="true"
    android:layout_below="@id/frag_1"/>

rmaik
  • 1,076
  • 3
  • 15
  • 48
  • You know, you're instantiating a new fragment, setting arguments on it and then discarding it. – laalto Jan 26 '15 at 10:42

1 Answers1

0

You have two Frag2 in your code.

This first one is created during the inflation time of your mainActivity.xml, and no arguments is passed to it when its onCreateView or onViewCreated method is executed.

The second one is instantiated in your code as following:

Frag2 frag2 = new Frag2();
frag2.setArguments(b);

Thus the argument you set here has nothing to do with the prior one that is attached to your Activity.

Here is the solution to you problem:

  1. Add a public method to your Frag2 class as following:
   public void passValues(float x, float y, float z){
    this.x = x;
    this.y = y;
    this.z = z;
    View view = getView();
    if(view != null){
         // set the value to views here.
    }

   }
  1. Change the onSendValues method in your MainActivity as following:
@Override
public void onSendValues(float x, float y, float z) {
    Frag2 frag2 = (Frag2)getFragmentManager().findFragmentById (R.id.frag_2);
   frag2.passValues(x, y, z);
}
Lei Guo
  • 2,550
  • 1
  • 17
  • 22