Android - handle onPause, onStop in specific way - android

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?

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.

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.

Activity in Android

As I am new to android, trying to learn the basics in detail. Basically I am an iOS programmer. I would like to know how the "Activity in android" is compared with iOS. That means, what is Activity in android when comparing with iOS.
Thanks in advance.
Check out the Documentation for Activity All of these are in there, and many of them contain more detail than what I've listed here.
1.This hook is called whenever the content view of the screen changes (due to a call to Window.setContentView or Window.addContentView).
2.Called when activity start-up is complete (after onStart() and onRestoreInstanceState(Bundle) have been called).
3.Called when activity resume is complete (after onResume() has been called).
4.This hook is called whenever the window focus changes.
5.Called as part of the activity lifecycle when an activity is about to go into the background as the result of user choice. For example, when the user presses the Home key, onUserLeaveHint() will be called, but when an incoming phone call causes the in-call Activity to be automatically brought to the foreground, onUserLeaveHint() will not be called on the activity being interrupted. In cases when it is invoked, this method is called right before the activity's onPause() callback. This callback and onUserInteraction() are intended to help activities manage status bar notifications intelligently; specifically, for helping activities determine the proper time to cancel a notfication.
6.Called whenever a key, touch, or trackball event is dispatched to the activity.
7.Called when the window has been attached to the window manager.
Although there is no 1:1 match for an Activity in iOS SDK, UIViewController is the closest match.
Beginning Android for iOS Developers can help you to cover some ground in writing your first Android app.
The Activity in Android is like the controller in iOS. It receives UI events, interacts with the data model, and updates the UI.
better to read this :
http://developer.android.com/reference/android/app/Activity.html

How to prevent call of onDestroy() after onPause()?

In my android application I have noticed that on press of back key, after onPause() automatically, onDestroy() is getting called.
How do I prevent the application from calling onDestroy() after onPause()?
I dont want to destroy the instances after back key press.
On press of Back key, my webview object is getting destroyed. So, I am not able to access the webview again,once I press back key.
I have got two URLs. Imagine them as URL1 and URL2.
When I launch URL1 in the webview and press back key the flow is onKeyDown() -> onPause()
When I launch URL2 in the webview and press back key the flow is onKeyDown() -> onPause() -> onDestroy()
Why is there a difference in the behaviour? Has it got anything to do with cookies?
For URL1 -- cookies.isSecure=true;
For URL2 -- cookies.isSecure=false;
Check the declaration of your activity in Manifest. Declare it in such a manner that it can store the data when minimized.
Activity should have the capability to restore the content.
That's how the Activity lifecycle is designed in Android and you should not interfere with it. You app can be destroyed by runtime at any point of time regardless of you want it or not :) So a better approach would be to accommodate the lifecycle in your application's logic.
you can override finish() to avoid this:
#Override
public void finish() {
//super.finish(); // do not call super
moveTaskToBack(true); // move back
}
you can override onBackPressed() in your activity, but that should be the last resort,
get a hang of these links before you start
activity lifecycle
developer blog
Normal android application behaviour states that the Home button hides the app to the background (you'll be shown the home screen), and Back button finishes the application (goes through onPause, onStop, onDestroy, in that order).
If you want to retain instances/states of your application when back button is pressed, I suggest you do the saving in onPause - save the states in SharedPreferences or in a place in your sqlite db. But mind that the saving process should be as quick as possible, because your application will wait until onPause executes completely before exiting.
Then load your instances/states in onResume.
You can know that onDestroy() will be called after onPause() using isFinishing() so you can avoid some code.
The answer here specifies that we have to configure the manifest in such a way that it should not get destroyed, but it does not answer how to configure. I arrived at this post when I had a similar problem for, the solution is pretty simple. Just mention
android:persistent="true"
Reference : - https://developer.android.com/guide/topics/manifest/application-element.html
This should prevent your activity getting destroyed when you click back button
I explained in detail with a use case in another post,
Prevent activity from being destroyed as long as possible
I hope this helps others who arrive at this place for the same problem

variables retaining values after app close

My app is retaining all of the variable values when it closes and this is effecting how it runs when reopened. Is there any way to reset them all upon app close? or is there a way to clean the app from memory when it is closed so to speak? For the moment I have just been setting all of the important variables "=0" in the last few lines of execution but I know there must be a correct way to doing this.
EDIT:
OK I thought that it would just be easier to reply here instead of individually to everyone.
The app is indeed staying alive in the background, I checked with advanced task killer. How would I get the ap to "Die" by presing the back button? I think this would be the easiest solution given how the app works:
open app > press start button > press stop button > results screen > press back button to exit.
so basically each time the app runs should be an independant execution.
Override the onPause, onResume, and onDestroy methods. onPause should save anything upon pausing, onResume should reload these values when it is resumed, and onDestroy will be called when your app closes. You can clean up stuff in onDestroy. See this link.
You app is probably not closing but remaining in background. Check advanced task manager and see if the app is running or not.
You need to familiarize yourself with the Activity Lifecycle.
You could leverage onResume() to reset your variables; also note onDestory() and onPause().
UPDATE:
Killing the application in its entirety each time the app moves to the background is an anti-pattern. You should really look at your application and follow the aforementioned activity lifecycle pattern and take the needed steps to insure your variables exist as you desire based on state.
I like what #Alex and #Jack said. To add to that, also consider that you can call finish() in your Activity if you want to force it to close up and return to the last Activity. Going along with this, also consider the use of setResult(int) (JavaDoc Here)
You can also set a flag on the Intent when you call the Activity you are questioning about. A flag like FLAG_ACTIVITY_NO_HISTORY could be helpful:
If set, the new activity is not kept in the history stack. As soon as the user navigates away from it, the activity is finished. This may also be set with the noHistory attribute.
List of Intent Flags
Uninitialized variables are bad. Don't do it. ALWAYS manually reset variables before using them for the first time.
the onResume() method will let you reset the variables when the program resumes, but will also do it when you return to the activity unless you add the logic that says you are coming from in the app, not the home page. Maybe onRestart() is what you really need? I'm not positive, but it's possible with onResume.

Categories

Resources