How to pass a boolean between intents
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);
}
});
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");
}
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