Does the Back Button fail to call stop()? - android

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.

Related

Android onCreate flow

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.

Distinguish activity calling cases: from other activity/other package/by system

I'm making a simple e-book reader app, and an activity can be called by many cases.
I'd like to distinguish callee activity to know its origin action in case of
From my another activity: this can be easily solved by
StartActivityForResult from calling activity.
Called by back button click from other package app after share action ("whoops, I missed to click share button, and then back.").
Switched by user's multitasking choice.
Called by user click at the start screen: this might be known by MAIN entry point at the android manifest.
How to know above cases?
I have no idea why you would need to do this but...
1.From my another activity: this can be easily solved by StartActivityForResult from calling activity.
Yes, as long as the calling Activity is your own as you can't guarantee any 3rd-party code will use startActivityForResult(...). You can, however, use getCallingPackage() and getCallingActivity() in other cases.
2.Called by back button click from other package app after share action ("whoops, I missed to click share button, and then back.").
When the user presses the BACK button your Activity isn't being "called" - it's simply being resumed or re-started. The original calling app/Activity/method will still hold true - there is no way to ascertain that this has happened as the normal Activity life-cycle methods (onStart() and onResume()) are always called even when an Activity is first created.
3.Switched by user's multitasking choice.
If you mean using the "Recent" apps view, the same applies for my answer to 2. above.
4.Called by user click at the start screen: this might be known by MAIN entry point at the android manifest.
In this case onCreate() will be called although if your Activity is simply stopped for whatever reason, it may simply be restarted depending on the launch mode you use.
In short, I can't see you being able to gather much in the way of any accurate information as to how your Activity becomes active.
I am not too sure about the actual way for the above question as I am too a new guy in android.
But to the best of my knowledge... called by back button and switched by user's multitasking leads the activity to enter pause state.
So you can access it from "onPause" method in your activity.

Subsequent Launch after uncalled onDestroy shows a blank screen

I am using a WebView in my app's sole Activity. Whenever the user clicks on the BACK button, I override the onKey and process some clean up before calling finish of the activity.
I see that once in a while (maybe 1 out of 20 times), onDestroy is not called. And in this case, if I launch my app again, a blank screen comes up. The Activity's onCreate is not called, and neither is my overridden Application's onCreate.
Does anyone know why this happens, and are there any possible solutions?
Thanks,
Rajath
Based on the little less than a line of code you have provided, please do the following:
Stop overriding back key.
Do the cleanup elsewhere, e.g., in onPause().
It turns out that I was capturing uncaught exceptions using the UncaughtExceptionHandler(). So, sometimes when the app was actually crashing in onDestroy(), the subsequent session was not properly launched.
Of course, in the case where the app's onDestroy() was not called at all, the problem is still fixed when I don't use UncaughtExceptionHandler(). Not sure why though.
-Rajath

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.

Android - handle onPause, onStop in specific way

I need to log action in my app. Action is startApp, stopApp, pauseApp and resumeApp. Tricky part is that my approach is diffrent than standard Android way. When I say startApp I need start aplicaton, stopApp is when all Application goes background(ex. hit home button). Pause is when something force to pause App ( but don't want log when I lunch another activity from my app ). So startApp != onStart() , rather Application.onCreate(), stopApp != onStop() , pause != onPause() etc....
Has anybody idea how to handle this ?
I think about put KeyEvent on "Back Button" in first activity to determine if app is stop. But how about Home Button ? I can't use it the way I use "Back Button". How about pause ? I think about use standard onPause() and inside this method try to recognize if onPause() is invoke by my another Activity or by for example phone call. But how to recognize what invoke onPause ?
Thanks for any suggestions.
It's probably not a good idea to override what the home button does. Users will always expect it to do the same thing, so you want to keep that experience consistent.
I would recommend finding a way to do what you want to do within Android's given life cycle methods. So, in onPause you could use a flag of some sort to denote whether to do your own stuff or to handle the regular Android way. Does that make sense?

Categories

Resources