Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a boolean between intents

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();
}}
like image 797
Carnivoris Avatar asked Jul 19 '11 17:07

Carnivoris


People also ask

How do you pass an activity in intent?

To pass the data through Intent we will use putExtra() method and in parameter, we will use Key-Value Pair. Now, where we have to mention putExtra() method? We have to add putExtra() method in onClick() as shown in the below code and in parameter we have to mention key and its value.

What method adds or passes data to the intent?

You can add extra data with various putExtra() methods, each accepting two parameters: the key name and the value. You can also create a Bundle object with all the extra data, then insert the Bundle in the Intent with putExtras() .

What is intent parameter?

Intent parameters are used to identify and extract values within training phrases. These are specific words or phrases you want to collect from the user. Parameter name: The name associated with the parameter. This is used to reference the parameter in slot filling for scenes.

What is the main use of the extra data in an intent?

Whenever you need data from an activity to be in another activity, you can pass data between then while starting the activities. Intents in android offers this convenient way to pass data between activities using Extras. Creating multiple activities to display contents of same properties is not an ideal solution.


3 Answers

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");
}
like image 73
citizen conn Avatar answered Sep 23 '22 19:09

citizen conn


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);
  }
});
like image 44
vol Avatar answered Sep 26 '22 19:09

vol


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

like image 42
Aqua Freshka Avatar answered Sep 24 '22 19:09

Aqua Freshka