Using Bundle to move a value from activity to activity but can't get them from on create method?

MarbleMad picture MarbleMad · Aug 31, 2011 · Viewed 10.1k times · Source

My java skills are not strong. Only been programming in it for a month or 2 so forgive my stupidness.

I'm trying to pass values between methods in a bundle to allow me to save and load some game settings but although I think my values are transferring, I can't get a value out of the 'on create method' to use in the rest of my programme.

I'm loading and bundling up my boolean value here (I've snipped out lots or hopefully irrelevant stuff) :

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
vV = new VortexTouch(this);     
CONTEXT = this;
// LOAD DATA
SharedPreferences settings = getSharedPreferences("GAME_DATA",MODE_PRIVATE);
_dPad = settings.getBoolean("GamePad", true);

// PASS DATA
intent = new Intent();
intent.setClass(this,VortexRenderer.class);
intent.putExtra("my_data", true); // should be _dPad but put 'true' in there for now.
startActivity(intent);
// PASS DATA END      

setContentView(vV);
}

The boolean value is then received in my VortexRenderer class:

public class VortexRenderer extends Activity implements GLSurfaceView.Renderer {

private static final String LOG_TAG = VortexRenderer.class.getSimpleName(); 
@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState); 

Bundle bundle = getIntent().getExtras();
_dPad = bundle.getBoolean("my_data");
_dPad = true; // testing to see if value carries, it doesn't :-( 

finish();
}

public boolean _dPad;

public void SomeGameAction(){
//try to do something with _dPad but it has not taken on a value of true. why? 
}

so i think the value of _dPad is getting from one activity to the other but it's not getting out of the VortexRenderer 'onCrate' method.. Clearly I'm not understanding something.. can anyone help? Thanks.

My game was built around this excellent tutorial if that helps (not that there's much left of the original now): http://www.droidnova.com/android-3d-game-tutorial-part-i,312.html

Less helpful but if you're interested this is what I'm trying to add the code to: https://market.android.com/details?id=com.clockworkrobot.spacewarz

Answer

Raider picture Raider · Aug 31, 2011

In the first activity, instead of

intent.putExtra("my_data", true);

use

Bundle bundle = new Bundle();
bundle.putBoolean("my_data", true);
intent.putExtra("android.intent.extra.INTENT", bundle);

Then in the second activity, instead of

Bundle bundle = getIntent().getExtras();

use

Bundle bundle = getIntent().getBundleExtra("android.intent.extra.INTENT");