开发者

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

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
p开发者_运维知识库ublic 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


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");


I have to be honest... I'm not sure what you're truly trying to do here.

Calling finish in your onCreate will end your activity as soon as you start it.

You do appear to be passing/receiving the boolean with the intent properly, but then hardcoding _dPad to true I hope is just for debug because it certainly makes it unnecessary to pass it with the intent.

What is the overall purpose of your VortexRenderer activity? I imagine there will be a better way to accomplish your goal without creating a new activity.

I also recommend using the Log.v(tag, message); utility and logcat to help yourself debug challenges.

Code indentation will also most certainly help code readability.


The best way that works for me every time is that you use Global Static class to hold your temporary data. Use setters and getters it will be much more easy and understandable.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜