Jump to content
  • Advertisement
Sign in to follow this  
ErikNavarro

Android Dev Problem

This topic is 2773 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 ?

Share this post


Link to post
Share on other sites
Advertisement
Hidden

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 ?

Share this post


Link to post
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 :/

Share this post


Link to post
Share on other sites
[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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!