How to pass a boolean between intents

风流意气都作罢 提交于 2019-11-27 22:03:24

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);
  }
});
citizen conn

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");
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!