Hi
What are the android life cycle method executed when an activity is started from another activity and also the method that should be executed in corresponds to home button click.
Is there any way to detect that user has pressed a home button ?
is there exist any unique method thatexecute as part of home button,am not meaning home button listener?
Android Activity lifecycle. OnCreate is called on the new activity, onPause on the old one.
You can't capture the home button press, or do anything with it, but onUserLeaveHint is called as an fyi.
Related
i'm trying to show an add on my app .
i want to show the add every time that the user launches the app or if the app is in the background when the user relaunch it.
i added the function that shows the add in the onRestart() event and it seems to work fine.
my problem is that if the user goes back to a previous activity by pressing the android Back button it triggers the onRestart() event as well.
is it possible to determine if the activity was loaded by the back button?
Thanks alot
Avi
From what I understand, here is what you can do:
If you are starting an activity from another activity, instead of calling startActivity(), use startActivityForResult(). In the called Activity, override the onBackPressed(). Put some data in the returned intent and call super.onBackPressed(). In the caller activity you will then analyze this to know that back was pressed :)
Also, move the advertisement to the onResume() method which is called when the Avtivity becomes visible to the user.
I suggest you to put your add in the Onresume() method of your main activity;)
I want to play audio on lock screen and I know for this I have to use service. But I am little confused So I have following questions.
I want to know on home and back button press which method is called i.e. onPause or onStop?
If my app running and someone call me and I pick the call then which method is called i.e. onPause or onStop?
On screen lock which method is called?
first onPause() will be called for each of your question and onStop() will be called when another activity comes in foreground and your previous one goes in background.
The Basic difference is that when onStop() called the previous activity in not visible and new activity comes in foreground and overlap the previous one.
On home press: onPause->onStop. On back press: onBackPressed->onPause->onStop->onDestroy
Same as pressing home essentially.
Same as 2.
Have a look at the documentation:
http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
I have a FragmentActivity. Within its onPause function, I would like to differentiate
Home button pressed
Back button pressed
Launching a new activity (This will cause the fragment activity's onPause being called)
For back button pressed case, I know I can differentiate it by using this.isFinishing() == true.
However, how about launching a new activity case?
I know perhaps I can set a flag before launching the new activity, and reset the flag, at the end of onPause function. But, it doesn't sound elegant to me. Is there any better and robust way?
You can create a singleton class that tracks when your activities start/resume/pause/stop. Make a base Activity class that calls the singleton in each of its lifecycle callbacks.
If you look at the order of the callbacks when transitioning between activities, you should see this:
Pause Activity A
Create Activity B
Start Activity B
Resume Activity B
Stop Activity A
When your singleton gets called from Activity A's onStop(), you can check if there was a call from onResume() from another Activity (which clearly belongs to your app). If there wasn't, you know the user has changed apps or gone back to the home screen.
As for the back button press, you can check isFinishing(), or override Activity.onBackPressed() and do your bookkeeping there.
I need to simulate a part of the lifecycle, from onPause to onResume event.
I used the back button to pause the app, when I enter the app again, it always to the onCreate event to start a new lifecycle.
How to make the app run from onPause to onResume directly ?
Thanks.
well android use back button to remove activity from stack.
but if you don't want to remove activity from stack you can use home key for returning from app.
but if you still want back button to work like home button then you can refer here
try opening a dialog on press of a button. This will call the onPause. Close the dialog onResume will be called. Start a new activity onPause followed by onStop called. Press back onResume called
I have a home screen widget with a button with a pendingIntent. Click it, it bundles a boolean to indicate where it came from and it brings up my main activity.
In my main activity in onCreate, I check for a bundle, if the boolean that says it was launched from my widget is true, I perform an action.
Every time I hit my widget button, it should launch my main activity, and run through onCreate.
And it does, on the emulator. And it did, on my n1. Then suddenly it stopped on my n1. I can still get it to hit onCreate when I launch from the widget but I have force stop or reinstall and then it only does it the first time.
What is going on with this?
What's going on is the Activity lifecycle. Android will keep the same instance of your Activity around so that the user can return to it later. Monitor some of the other lifecycle methods like onStart and onResume to see this in action.
You may see a difference in behavior if the user hits the back button vs. the home button. The back button will finish the current Activity by default, whereas home will leave it alone unless Android decides to kill it for resources later.