Android Dev Problem

Started by
5 comments, last by ErikNavarro 12 years, 11 months ago
private OnClickListener mClickListener = new OnClickListener() {

@Override
public void onClick(View v) {
if (v.getId() == R.id.btnNext) {

Double taxPercentage = Double.parseDouble(txtTax.getText().toString());
taxMult = ((taxPercentage / 100)+1);
int numPeople = Integer.parseInt(txtPeople.getText().toString()) ;

nameArray = new String[numPeople];
totalArray = new double[numPeople];



while (!(counter == numPeople)){

Intent myIntent = new Intent(RogCalc.this,UserData.class);

RogCalc.this.startActivityForResult(myIntent,1);

counter = counter + 1;
}
txtAmount2.setText("DEBUG");
}
else {
reset();
}


}
};


My question pretty much comes down to this : txtAmount2.setText("DEBUG");

Why does this code run right when I click the button ? Why doesn't it wait until all the activity's have ran and the while loop has finished ?
Advertisement
startActivityForResult does not block.
You will be called back when the activity ends. There are explanations/ideas here.

private OnClickListener mClickListener = new OnClickListener() {

@Override
public void onClick(View v) {
if (v.getId() == R.id.btnNext) {

Double taxPercentage = Double.parseDouble(txtTax.getText().toString());
taxMult = ((taxPercentage / 100)+1);
int numPeople = Integer.parseInt(txtPeople.getText().toString()) ;

nameArray = new String[numPeople];
totalArray = new double[numPeople];



while (!(counter == numPeople)){

Intent myIntent = new Intent(RogCalc.this,UserData.class);

RogCalc.this.startActivityForResult(myIntent,1);

counter = counter + 1;
}
txtAmount2.setText("DEBUG");
}
else {
reset();
}


}
};


My question pretty much comes down to this : txtAmount2.setText("DEBUG");

Why does this code run right when I click the button ? Why doesn't it wait until all the activity's have ran and the while loop has finished ?
I can see texAmount2.setText() happen before the subactivity has time to visually[font=arial, sans-serif][size=2] [/font]take the place of the main activity. ( the emulator is slow enough that I can see this happen )

Any help would be greatly appreciated. This is driving me crazy :/
[font=arial][size=2]
It looks like Sri Lumpa is right. StartActivity or StartActivityforResult are asynchronous calls that don't block the UI Thread (which calls the onClick() callback function). I also suspect that the newly spawned activities are waiting for the UI Thread to return before they can take over.

So it looks like the expected behavior would be:
UI Thread calls onClick() callback -> Asynchronously spawn numPeople activities -> set the debug text -> UI Thread's onClick() callback returns -> The top activity in the activity stack gains control of the UI

On another note, if the UI thread takes too long to return (if another activity was blocking it for example), then Android would believe the UI has frozen and will throw an ANR (Application not responding) so that the user can force close the app.[/font]
As SriLumpa says, startActivityForResult doesn't block - it works asynchronously, so that processing continues in the caller as well as in the new Activity. Try putting in a counter that counts the number of iterations of the while loop, and displaying that - you should find that the while loop does run first, it's just that this doesn't mean what you think it does.

If you need something to happen when the Activity returns, then put it in the callback.

EDIT: Ninja-d.
[TheUnbeliever]
Why are you using a while loop instead of a for loop?
public void onClick(View v) {
if (v.getId() == R.id.btnNext) {

Double taxPercentage = Double.parseDouble(txtTax.getText().toString());
taxMult = ((taxPercentage / 100)+1);
int numPeople = Integer.parseInt(txtPeople.getText().toString()) ;

nameArray = new String[numPeople];
totalArray = new double[numPeople];

int i;



for (i = 0; i < numPeople; i ++ ){

Intent myIntent = new Intent(RogCalc.this,UserData.class);

RogCalc.this.startActivityForResult(myIntent,1);

}
}


Here's my new and improved code. I didn't realize that[font="arial"][color="#1c2837"] StartActivityforResult() was asynchronous.[/font]
[font="arial"][color="#1c2837"]
[/font]
[font="arial"][color="#1c2837"]I believe the code above creates all the sub-activity's at once ( in the for loop ). Each one is then handed control of the screen as the previous one exits ( and returns a result )[/font]
[font="arial"][color="#1c2837"]
[/font]
[font="arial"][color="#1c2837"]Anyway, it works ![/font]
[font="arial"][color="#1c2837"]
[/font]
[font="arial"][color="#1c2837"]Thanks for the help =][/font]

This topic is closed to new replies.

Advertisement