Android onCreate flow - android

I'm new to android development and working these days on my first app.
In my activity, in the onCreate function I make 2 function calls , each of the 2 functions has different OnClickListener for different buttons.
Everything works fine , my question is why?
I mean how does it possible that both of the listeners functions "work" on the same time does the app runs in a parallel way? In my head I thought that once the app reach an OnclickListener it will just stop and wait for a buttonClick event but I can see that I can click on button1 and something happens and then I click on Button2 and something else happens (as it should).
I just want an explaination, in general, about the way (flow) the app work/ handles functions of listeners and click events.
Thank you,
Noam

I totally understand what you mean.
button.setOnClickListener(this) means this button just tells the activity that it has a click event. The app won't pause or wait until the button event is executed.
when different widget is clicked, it's own click event will be executed.

Related

Does the Back Button fail to call stop()?

My app currently has only one Form, which listens to the accelerometer sensor. In my start() method, I turn the listener on, and in the stop() method, I turn it off. I have verified that the listener turns off when I hit the Android's home button, but when I hit the back button, the application exits and the Android returns to the home screen, but the listener keeps going, which means the stop() method never got called. Is it my responsibility to handle the back button with code to call the stop() method? Or is this a bug in CodenameOne's framework? It seems to me that when the back button returns the user to the home screen, it should call stop() for me.
I am not sure about all the details of your issue, however you can resolve it by calling the setBackCommand on that one form.
yourForm.setBackCommand(
new Command("closing the sensor listener"){
#Override
public void actionPerformed(ActionEvent ev){
// your code to close the listener
}
}
);
I don't know about CodenameOne framework, but When app is visible and you press back button it calls all four methods in following order
1)onBackPressed()
2)onPause()
3)onStop()
4)onDestroy()
and when you press home button it calls only
1)onPause()
2)onStop()
methods
so, when you press back button onStop must be called.
You please put source code so that people can understand your problem clearly.
I don't know about CodenameOne framework, but I know the Android SDK.
Activity.onBackPressed() should be invoked when you use the back button. Just because your Activity is no longer visible does not mean it has been reaped, and this might explain why Activity.stop() is not invoked (immediately).
Depending upon your use case, Activity.onPause() might also work better.
HTH. Good luck w/your project.
This has been fixed. I've verified it, and it works correctly now.

Can Android have multiple listeners?

Say I have 5 buttons. For each button I want to be able to fire a listener.
Once the listener is fired I want fire an async task in my sdk, and then have the sdk return the status of the async task.
1) Do I write a separate listener/ button? I read somewhere I can have only one registered listener in android, if thats true how do I handle many listeners?
2) How do I return postExecute call result from SDK to the api level?
1) Do I write a separate listener/ button? I read somewhere I can have only one registered listener in android, if thats true how do I handle many listeners?
A View can only have one listener of each type, ie a Button cannot have two OnClickListeners. Don't confuse this with the fact that one listener can be attached to multiple Views, ie ButtonA and ButtonB can have the same OnClickListener
2) How do I return postExecute call result from SDK to the api level?
Your terminology isn't right, but you'll figure it out as you go. Typically onPostExecute() will call another method or work directly with a View:
#Override
protected void onPostExecute(String result) {
doSomething(result);
textView.setText(result);
}
Easiest is if you setup one listener in your activity, and then handle multiple buttons. You can do this with the OnKeyListener class. You then do a switch on which key was hit, setup cases for the buttons you wish to act on, and start your AsyncTask.
I'm not quite sure what you mean by "return postExecute". But if you look at AsyncTask you can see how to use the proper parameters to return a result into onPostExecute. When you instantiate your subclass of AsyncTask you can easily pass in the activity or context you wish to perform a call back on.
You can have multiple listeners and depending on what you want to do with them, you can have one for each button. As far as the postExecute(), it runs on the UI thread so you can show your result from there or do whatever you want with it. For more details, you will need to provide some of the code you have tried and explain exactly where you are having trouble. If you haven't already, go through the
Android Docs about getting started
This link describes AsynTask

ViewFlipper, onClick events not delivered

I have my own implementation of the ViewFlipper (that exactly mocks the Android code, I wrote it before I realized this), the only difference being the fact that I hardcoded an inAnimation and an outAnimation in mine.
One side of the ViewFlipper has a 'flip' button which flips. The other side has a 'save' and 'cancel' button which flips it back. The 'save' performs a DB operation.
When the save or cancel, it flips the card correctly. If I perform the following operation: flip->cancel->flip->cancel..., it works fine. But when I perform: flip->save->flip, the last flip is non-response and logcat shows me that the touch operation was not delivered because of a timeout. The first thing I checked and ensured was that the database operation was not holding off the UI thread, and it was not!
I use the content of the ViewFlipper (using the View.getContent()) to perform DB operations, throw Toasts, build Alert Dialogs and the like. Might this create issues?
I've read a post somewhere saying that there was an issue with the ViewFlipper with animations and onClick() events not being delivered (the discussion ended with no solution). Am I a victim of this?
Try doing the save operation in a thread even though you are sure its not blocking the UI thread.
If that does not work, then set onTouch listener to the save view.

Android onClick event fails

I'm having some problem here with android + java + cloud. My onCLick event is working only one time
The event only work again when i go back to the last screen and enter again in the screen that i made searchs in the cloud...
I think you are only allowed to call setContentView() once. Since you are doing it in your click listener that might be why it is failing after the first time.
Edit: What does it do when it fails? Force close? nothing? something unexpected?

Android - Detect 'app became active' from multi-tasking

I am trying to detect if my app became active from multi-tasking and display a dialog.
The use-case is something like this: User is using the app -> Hits the home button -> Does something -> User taps on the app again
As expected, the last activity the user was on is being shown.
I want to display a dialog when this happens. I used onRestart() for the same and it works. Only problem is, even when the user goes to some activity in the app and then hits back, the dialog is being displayed.
I went through the activity lifecycle several times, but couldn't figure out the solution. Any ideas?
Well, you can tell the foreground application using the technique outlined in Determining the current foreground application from a background task or service: look for getForegroundApp. You could run a timer that checks periodically to see if the app is foreground, and if its not then set a variable after a suitable delay (to make sure you don't happen to hit it in the wrong order when switching Activities). Clear the variable in onStart, and if onCreate of the rooth Activity is ever called you know that the app just became Active.
I achieved this by setting a flag in Shared Preferences in onStop() and cleared it in onDestroy().
Then I overrided the Back button to clear the flag whenever it is pressed. This solves the problem I had stated.
Now in onRestart(), if the flag is true.... I display the dialog.
I know it is not the most elegant solution but does the job! Hope this helps somebody.

Categories

Resources