Using Bundle to move a value from activity to activity but can't get them from on create method?
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");
MarbleMad
Updated on June 04, 2022Comments
-
MarbleMad almost 2 years
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
-
MarbleMad over 12 yearsThat makes it crash :-( If I comment out: _dPad = bundle.getBoolean("my_data"); it runs but again that _dPad=true is not carrying over to the rest of my app.
-
Raider over 12 yearsSorry about that. I missed the first part. You need to add the data to a bundle and add the bundle to the intent. See my updated answer.
-
MarbleMad over 12 yearsRight.. that stops the crash however I'm still not getting my _dPad variable to change to 'true' am i mishandling that variable somehow? or is it that finish(); that's wrong? (without it i just get a black screen). Thanks for the assistance.
-
Raider over 12 yearsCalling finish() will destroy the activity, which probably isn't what you want to do in onCreate(). Make sure you call setContentView() after super.onCreate().
-
MarbleMad over 12 yearsin essence I've built a game based on this tutorial. droidnova.com/android-3d-game-tutorial-part-i,312.html I've massively modified it and retro fitted sound and tilt detection but the structure is basicly the same. All I'm trying to do now is retrofit some way of saving a few game setup variables (eg whether you prefer to play with a virtual _dPad or instead tilt the phone ) but the problem is really kicking my bottom. I'm in a right tangle.
-
MarbleMad over 12 yearseliminated the call finish and added an onCreate() appear to have 2 copies of my game running now, one with no tilt control. I'll have to study that more, it's obviously duplicating something from another class. Yes the _dPad=true; is just debug. my VortexRenderer class contains all the game logic and the GL render stuff. I'll look into Log.v .. thanks. I have code indentations but this was my first post here and i botched the formatting a bit.
-
MarbleMad over 12 yearsThanks I'll give that a go as I'm getting nowhere with the bundles.
-
Jerry Destremps over 10 yearsAvoid globals wherever possible. Not thread safe and not best practice.