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
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 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'd like to destroy an Activity when the user clicks on a button (not the back button, a different button). I've decided to just call super.onBackPressed. Is that okay? What's a better way to close the current Activity without closing the whole app?
for closing an Activity you can use finish() , but you have to aware of back stack, if you finish your last activity in the stack so there will be no activity in your stack and you must restart your app .
see more information on : Task and BackStack
The default implementation of super.onBackPressed finishes the activity.
Instead of using super.onBackPressed , Better way to close the current activity will be to call finish() method.
Also, When you press the back button, the onResume method is called.
Use onResume Method and refresh your stuff there.
Also, if it is listView refresh the data -> https://stackoverflow.com/a/12662994/2006412
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.
when application installed and opend first time,and i pressed home key then application minimises normally.but when i starts same application(not minimised one),it shows me blank screen.this happens only first time.i am not getting what happens..can any body help
When you starts a fresh application, it first call onCreate then onStart then onResume
When you press home key it will call onPause then onStop
So when you start it again (by clicking its icon) it call onRestart then onStart then onResume, will not call onCreate here
So if you feel some different behavior, then check the code in any of these function if you Override them
Refer this link also http://developer.android.com/reference/android/app/Activity.html