• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

activity doesn't wait untill alertdialog completes

 
sam liya
Ranch Hand
Posts: 1243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a main activity.In that activity i called to alertdialog activity.On that alert dialog i give the users to select one from two option.


this is alert dialog activity.



this is working fine.Anyway if added the below two line end Oncreate method of the main activity it doesn't wait untill the alert dialog finishes.it goes to contact activity .So i don't have chance to select option from alert dialog.how to avoid this situation?

 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right: dialogs in Android are asynchronous, they don't stop further code execution. You need to move that code into a separate method that is called only after the user has made her choice.
 
sam liya
Ranch Hand
Posts: 1243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I change the class as below.But it doesn't go to Conatct.xml even i select POST in alert dialog .That mean when control goes to onActivityResult() method it doesn't come to the oncreate method ?I need to know why ?I used debug also ,it doesn't come to the oncreate method.

 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For starters, strings are compared using the "equals" method, not with the "==" operator.

When I said "separate method", I meant exactly that - not the onCreate method.

And when I said "... that is called only after the user made her choice", I meant exactly that. How do you imagine a variable that is set long after onCreate has run could in any way influence the execution of onCreate (even if that were the right thing to do, which it is not)?
 
sam liya
Ranch Hand
Posts: 1243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have activity A which runs main method.I nned give user to selection.So i used radion buttin with separate activity.I use intent to call that activity.I assign user selection to global varible.The n i need to resumethe first activity .I change my code.So my first activity is this.



htis is my second activity



but it doesn't work as expected.first activity doesn't wait until second one finished.So what need to change in this code.please help.
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If Test expects a result back from Settings, then it should start Settings not with startActivity, but with startActivityForResult - that will allow Settings to pass data back to Test. And then that callback method should do whatever needs doing after the user has made her choice. That way there is also no need to use an Application class for passing around data.
 
sam liya
Ranch Hand
Posts: 1243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
its ok.but when the second activity returns values to first activity .then control goes to onActivityResult method.cann't we get control to the again oncreate method after finishing the onActivityResult method on first activity?
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sameera liyanage wrote:its ok.but when the second activity returns values to first activity .then control goes to onActivityResult method.cann't we get control to the again oncreate method after finishing the onActivityResult method on first activity?

No.
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... and the fact that you're asking the question indicates that you're not yet fully acquainted with the Android Activity lifecycle. I suggest you read through the javadocs of the android.app.Activity class, several times if need be - it explains all this in detail, particularly when the onCreate method will be called (and when it won't).
 
sam liya
Ranch Hand
Posts: 1243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ulf and Steve.Actually my problem is like this.I have develop mobile application.There are several network providers.So when the app runs i chcecked the network provider type in oncreate method.I used TelephonyManager->getNetworkOperatorName() to get network operator.Depend on that i used if else to select network operator.

If(A){

}else if(B){

}else if{

....

the problem is my now is ,i need to get the sim type.So i give user to chance select their sim type whether its Pre paid or Post paid.When user select the sim type i saved it .So my problem is now when i used above code it doesn't wait until user selection.I need a way to wait the code runnng until user select his choise.User choise must select only first time.Then i update the database.
If there is a better way to impliment this code please let me know.

 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isn't that what this entire topic is about? Please reread my posts carefully, and take the time to consider how you need to change your code in order to incorporate my suggestions.
 
sam liya
Ranch Hand
Posts: 1243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found this way.I moved all the other codes to another activity and call that activity onActivityResult method.is it correct ?
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds way more complicated than it needs to be.
 
sam liya
Ranch Hand
Posts: 1243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
anyway we can't spot oncreate methos ,
So first i check the whether user already exist in DB.

if (No)
so inside oncreate i go to another methos and get user choise.

while doing that oncreate may finished.So i have to remove other part of code from oncreate ,because they need the user choise.I moved them to other method.After user selection it goes automatically to
onActivityResult method.So i call the my other part of code there.Isn't ok ?

Else
dirctly called the other method


is this ok?
 
sam liya
Ranch Hand
Posts: 1243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf and Steve ,please help me.
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds OK. What happened when you tried it?
 
sam liya
Ranch Hand
Posts: 1243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ulf and Steve.It seems working fine.Anyway can you please give me some points to improve my code with performance and standards.below is my maib class code.

 
sam liya
Ranch Hand
Posts: 1243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf please help.
 
sam liya
Ranch Hand
Posts: 1243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is working fine now.I need to know how to improve the code to increase performance and standards.please help me.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sameera,

The forum isn't really the right place for code reviews. Doing so is time consuming and the people who post here are volunteers, and have their own jobs and projects to work on. You will be better off using the forum for specific questions and answers rather than getting code reviews.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic