How to pass a boolean between intents
Asked Answered
H

4

20

I need to pass a boolean value to and intent and back again when the back button is pressed. The goal is to set the boolean and use a conditional to prevent multiple launches of a new intent when an onShake event is detected. I would use SharedPreferences, but it seems it does not play nice with my onClick code and I'm not sure how to fix that. Any suggestions would be appreciated!

public class MyApp extends Activity {

private SensorManager mSensorManager;
private ShakeEventListener mSensorListener;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    mSensorListener = new ShakeEventListener();
    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    mSensorManager.registerListener(mSensorListener,
        mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
        SensorManager.SENSOR_DELAY_UI);


    mSensorListener.setOnShakeListener(new ShakeEventListener.OnShakeListener() {

      public void onShake() {
             // This code is launched multiple times on a vigorous
             // shake of the device.  I need to prevent this.
            Intent myIntent = new Intent(MyApp.this, NextActivity.class);
            MyApp.this.startActivity(myIntent);
      }
    });

}

@Override
protected void onResume() {
  super.onResume();
  mSensorManager.registerListener(mSensorListener,mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
      SensorManager.SENSOR_DELAY_UI);
}

@Override
protected void onStop() {
  mSensorManager.unregisterListener(mSensorListener);
  super.onStop();
}}
Hilda answered 19/7, 2011 at 17:41 Comment(0)
F
6

have a private member variable in your activity called wasShaken.

private boolean wasShaken = false;

modify your onResume to set this to false.

public void onResume() { wasShaken = false; }

in your onShake listener, check if it's true. if it is, return early. Then set it to true.

  public void onShake() {
              if(wasShaken) return;
              wasShaken = true;
                          // This code is launched multiple times on a vigorous
                          // shake of the device.  I need to prevent this.
              Intent myIntent = new Intent(MyApp.this, NextActivity.class);
              MyApp.this.startActivity(myIntent);
  }
});
Forsythia answered 19/7, 2011 at 17:56 Comment(0)
M
82

Set intent extra(with putExtra):

Intent intent = new Intent(this, NextActivity.class);
intent.putExtra("yourBoolName", true);

Retrieve intent extra:

@Override
protected void onCreate(Bundle savedInstanceState) {
    Boolean yourBool = getIntent().getExtras().getBoolean("yourBoolName");
}
Matsumoto answered 19/7, 2011 at 17:48 Comment(2)
getIntent() is deprecated now.Eldoree
@user7856586 Do you have an official source that says that getIntent is deprecated?Feverroot
F
6

have a private member variable in your activity called wasShaken.

private boolean wasShaken = false;

modify your onResume to set this to false.

public void onResume() { wasShaken = false; }

in your onShake listener, check if it's true. if it is, return early. Then set it to true.

  public void onShake() {
              if(wasShaken) return;
              wasShaken = true;
                          // This code is launched multiple times on a vigorous
                          // shake of the device.  I need to prevent this.
              Intent myIntent = new Intent(MyApp.this, NextActivity.class);
              MyApp.this.startActivity(myIntent);
  }
});
Forsythia answered 19/7, 2011 at 17:56 Comment(0)
T
3

This is how you do it in Kotlin :

val intent = Intent(this@MainActivity, SecondActivity::class.java)
            intent.putExtra("sample", true)
            startActivity(intent)

var sample = false
sample = intent.getBooleanExtra("sample", sample)
println(sample)

Output sample = true

Taciturn answered 28/10, 2019 at 11:11 Comment(0)
A
0

SendActivity:

val intent:Intent = Intent(this,ReceiveAcitivity::class.java)

intent.putExtra("BOOLEAN_VALUE",true) startActivity(intent)

ReceiveAcitivity:

val condition = intent.getBooleanExtra("BOOLEAN_VALUE",false)

Log.d("Condition","intent receive value:$condition")

Anthropography answered 5/8, 2023 at 7:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.