aspose file tools*
The moose likes Android and the fly likes Click in button not sense Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Android
Bookmark "Click in button not sense" Watch "Click in button not sense" New topic
Author

Click in button not sense

fahimeh hashemian
Ranch Hand

Joined: Aug 07, 2012
Posts: 85
Hi, I have two buttons in page and i want when clicked on the buttons, the speed of balls can be low or high. I have a timer that i initialize and run this in onResume() method. I call onClickListener method for button click checking, but now when i clicked on the buttons, no event happens. Why? How can i solve this?

in onClickListener method, i changed value of delay and period.

Thanks for help...




Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42643
    
  65
What is "onClickListener"? Have you made sure that its methods are being called? If so, how?


Ping & DNS - my free Android networking tools app
fahimeh hashemian
Ranch Hand

Joined: Aug 07, 2012
Posts: 85
Ulf Dittmer wrote:What is "onClickListener"? Have you made sure that its methods are being called? If so, how?


onClickListener is a method for sense click in button... I initialize two buttons in onCreate . I write my codes in below. Please read and help me, why my click buttons not sense?



Thanks for help
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42643
    
  65
Ulf Dittmer wrote:Have you made sure that its methods are being called? If so, how?


Do you see the logging output printing the correct values?
fahimeh hashemian
Ranch Hand

Joined: Aug 07, 2012
Posts: 85
Ulf Dittmer wrote:
Ulf Dittmer wrote:Have you made sure that its methods are being called? If so, how?


Do you see the logging output printing the correct values?


I see log output in onClickListener but i changed my program to this that i know do my values set in onResume method or not ! but loging output in onResume() method not shows. why?




Thanks for help
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42643
    
  65
That just tells you whether onResume is called (which it will be). It doesn't tell you whether the code that alters those values is called when you expect it to be called.

that i know do my values set in onResume method or not

I'm not sure what you're trying to say - the values are not set in onResume, they will be set whenever the onClickListener is called. The logging statements in lines 36 and 37 of the previous code should show you whether or not that code is being called.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

The buttons will change the delay and period inside the Activity, but have no effect on the scheduling of the task that is already submitted. You will need to cancel the old task and re-schedule it with the new values. You should also cancel the task and stop the timer in your onPause() method, since you create and start the timer and task in your onResume() (otherwise you will have multiple instances running.)


Steve
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

fahimeh hashemian wrote:I see log output in onClickListener but i changed my program to this that i know do my values set in onResume method or not ! but loging output in onResume() method not shows. why?

onResume() is called as part of the Activity lifecycle when it is about to be viewed... it is not called when you press buttons.
fahimeh hashemian
Ranch Hand

Joined: Aug 07, 2012
Posts: 85
Steve Luke wrote:The buttons will change the delay and period inside the Activity, but have no effect on the scheduling of the task that is already submitted. You will need to cancel the old task and re-schedule it with the new values. You should also cancel the task and stop the timer in your onPause() method, since you create and start the timer and task in your onResume() (otherwise you will have multiple instances running.)


How can i cancel old task and create new task ? please more explain and if you can write a sample code. Thanks...
fahimeh hashemian
Ranch Hand

Joined: Aug 07, 2012
Posts: 85
Ulf Dittmer wrote:That just tells you whether onResume is called (which it will be). It doesn't tell you whether the code that alters those values is called when you expect it to be called.

that i know do my values set in onResume method or not

I'm not sure what you're trying to say - the values are not set in onResume, they will be set whenever the onClickListener is called. The logging statements in lines 36 and 37 of the previous code should show you whether or not that code is being called.


loging output in lines 36 and 37 shows but i can't sense influence of this values in onResume(). I think onResume() not call when i change values of period and delay..

Thanks
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42643
    
  65
fahimeh hashemian wrote:I think onResume() not call when i change values of period and delay.

Right - why should it be called? Read the javadocs of the android.app.Activity class to learn about the activity lifecycle - including which method will be called when.
fahimeh hashemian
Ranch Hand

Joined: Aug 07, 2012
Posts: 85
Ulf Dittmer wrote:
fahimeh hashemian wrote:I think onResume() not call when i change values of period and delay.

Right - why should it be called? Read the javadocs of the android.app.Activity class to learn about the activity lifecycle - including which method will be called when.


Then how can i solve this problem and change value of period and delay in onResume() method? I read this link about Activity, I think onResume call when the program runs for the first time or when the program be pause or stop, and then program will be runs again and onResume call ... this is true? and another question: if onResume() call in these positions but why, when be changed the values in onSensorChanged(), onResume() method calls?? I am confused

Thanks for help
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42643
    
  65
The point is that onResume will *not* be called. You will have to move the code elsewhere where it *will* be called.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

fahimeh hashemian wrote:
Steve Luke wrote:The buttons will change the delay and period inside the Activity, but have no effect on the scheduling of the task that is already submitted. You will need to cancel the old task and re-schedule it with the new values. You should also cancel the task and stop the timer in your onPause() method, since you create and start the timer and task in your onResume() (otherwise you will have multiple instances running.)


How can i cancel old task and create new task ? please more explain and if you can write a sample code. Thanks...


What does the API tell you?
fahimeh hashemian
Ranch Hand

Joined: Aug 07, 2012
Posts: 85
Ulf Dittmer wrote:The point is that onResume will *not* be called. You will have to move the code elsewhere where it *will* be called.


but onResume call when changed value of sensor! where should i move my code that fix in period and delay in onResume()?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

fahimeh hashemian wrote:but onResume call when changed value of sensor!

What does that mean? What sensor are you talking about?

where should i move my code that fix in period and delay in onResume()?

You need code to execute when onResume() is called, and when the buttons are pushed. The buttons being pushed do not cause onResume() to be called (nor should they). So what should you do? Move the code to its own method. Call the method from onResume() and the OnClickListener's onClick() method.
fahimeh hashemian
Ranch Hand

Joined: Aug 07, 2012
Posts: 85
Steve Luke wrote:
fahimeh hashemian wrote:but onResume call when changed value of sensor!

What does that mean? What sensor are you talking about?

where should i move my code that fix in period and delay in onResume()?

You need code to execute when onResume() is called, and when the buttons are pushed. The buttons being pushed do not cause onResume() to be called (nor should they). So what should you do? Move the code to its own method. Call the method from onResume() and the OnClickListener's onClick() method.


Sensor value is values of changed accelerometer sensor. I write full of my code in mainActivity in below.

I think codes in onResume method calls in per period seconds( mTimer.schedule(mTimerTask, delay,period); ) and when i click in buttons, runs onClick method and changed period and delay value and should fix these values in onResume method because mTimer.schedule(mTimerTask, delay,period); in onResume method use delay and period and these value changed in onClick(). I hope you understand what you mean if you not understand please tell me that i more explain.

Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

fahimeh hashemian wrote:
Steve Luke wrote:The buttons being pushed do not cause onResume() to be called (nor should they).


No, I specifically said that the onClick event shouldn't do that! Don't do that. The onResume() method should be called only as part of the Activity Live Cycle, not arbitrarily by your code. Go back and read what I said, and do that.

And remember that you will need to cancel any tasks already running, but there is NO need to kill the timer each time, you can re-use it (it would be nice to purge it though).
fahimeh hashemian
Ranch Hand

Joined: Aug 07, 2012
Posts: 85

No, I specifically said that the onClick event shouldn't do that! Don't do that. The onResume() method should be called only as part of the Activity Live Cycle, not arbitrarily by your code. Go back and read what I said, and do that.

And remember that you will need to cancel any tasks already running, but there is NO need to kill the timer each time, you can re-use it (it would be nice to purge it though).


I understand that onResume() method called as part of the activity live cycle but why, when changed values of accelerometer sensor (mBallSpd), the changes fix in onResume method but when i clicked on buttons and run onClick method and changed values of period and delay, the changes not fix in onResume()... i am confused. please if you can write a sample code or give me a link that i can solve my problem thanks
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Is the onResume() method being called, or is the ball simply moving faster or slower? If the first, then it is likely because the OS is trying to change the layout from landscape and portrait as you move the phone - a side effect you should stop from happening. If the second, then it is likely because you change the ball speed variables, which the task uses, not the delay and interval, which the timer uses when submitting a task.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

fahimeh hashemian wrote:but why, when changed values of accelerometer sensor (mBallSpd), the changes fix in onResume method but when i clicked on buttons and run onClick method and changed values of period and delay, the changes not fix in onResume()


The mBallSpeed values aren't used in the onResume() method, they are used in the run() method for the TimerTask as it executes (it just so happens you define the TimerTask inside the onResume() method, and so structurally it appears in the onResume() method). Each cycle of the TimerTask, the run() method is called, and so those values are re-read. The delay and period values are used in the onResume() method when the TimerTask is submitted to the Timer, and are not re-read or re-used at any other time.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Click in button not sense