what method is called when Actionbar back/home button is pressed? - android

I have two activities first activity opens on app start and it holds fragment second activity is opened from that fragment, when I press home button second activity closes and in fragment methods onDetach -> onAttach -> onResume is called, but if I close second activity with finish() or onBackPressed() fragment is never detached just onResume() is called.So my question is what method is called when home button is pressed and is there possibility to use same method to close activity for example on button press? Basically what I need is that fragment would call onDetach -> onAttach when I closing second activity.

you can remove the fragment in e.g. onDestroy() of the activity, so the fragment will go through it's lifecycle methods

The activities onPause() is called when pressing the home or back button. I just tried out using finish() and onBackPressed() which results in onPause() being called, too.
However, I am not sure whether there are differences when using fragments.
Android training concerning pausing and resuming: https://developer.android.com/training/basics/activity-lifecycle/pausing.html

Related

onDetach not called for fragment?

I write an code to launch Activity A to Activity B. Both Activity A and B has fragment implementation.
Scenario: If Activity A frequently launch Activity B which contain Fragment, then most of times it missed Fragment.onDetach..I checked with log, normally it give me following override method log:
onAttach
List item
OnCreatView
onViewCreate then press device Back Button
onPause
onStop
onDestroyView
onDetach
now I press device Back button from Activity B which again launch Activity A then it launch Activity B and repeat same sequence frequently, then log sequence get change in following order:
onAttach
List item
OnCreatView
onViewCreate then press device Back Button
onPause
onStop
onDestroyView and repeat with
onAttach without onDetach
some times it repeat same behaviour after onPause also.
I am using
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container, fragment).addToBackStack(null).commitAllowingStateLoss(); to add fragment in Activity.
Is there anything I am missing..any suggestion ?
A fragment is detached after it is destroyed. what u have done is dettached directly after destroying view.
Remember destroying and destroying view are two different things in fragment.
So try onDestroyView, then onDestroy and then onDetach.

Life cycle of Android Activity after pressing Back button

I am little confused between the life cycle of two activities.
Suppose I have Activity A and Activity B.
B is called From A i.e A ----> B.
Now currently B is on the screen and I pressed back button. Here I want know:- is there any memory still available for B(Active) or B's memory is flushed(Inactive).
The following activity call back methods are called, after pressing back button.
onPause()
onStop()
onDestroy()
The activity is destroyed.
And it recreates when launched again. These are the callback methods when it launches again.
onCreate()
onStart()
onResume()
I know the answer is been accepcted, still if this helps someone I am putting it.
When app is opening for the first time, by clicking the Icon
onCreate()
onStart()
onResume()
When home button is pressed
onPause()
onStop()
when app is again opened by clicking the app icon or launched from recent
onRestart()
onStart()
onResume()
when app is opened and then back button is pressed
onPause()
onStop()
onDestroy()
The onDestroy method is called after back press. Then activity will be popped from the Activity back stack.
From docs:
If an activity is paused or stopped, the system can drop the activity from memory by either asking it to finish, or simply killing its process. When it is displayed again to the user, it must be completely restarted and restored to its previous state.
onDestroy() from docs:
The final call you receive before your activity is destroyed. This can happen either because the activity is finishing (someone called finish() on it, or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing() method.
Activity B will be destroyed and will no longer remain in memory.
For more information please visit the official documentation for android and have a look at the activity life cycle figure.
Once you press the back key the activity's onDestroy() method will be called and the activity will be flushed out of the memory. You will then be required to restart the activity by calling the startActivity() method which will in turn call its onCreate() Method.
I would suggest to refer following link for activity lifecycle
http://stackoverflow.com/a/8516056/3110609
and following link for launch mode of activity.
www.intridea.com/blog/2011/6/16/android-understanding-activity-launchmode
After pressing the back button, Activity B will b destroyed. You see, Android Manages Activities like a Stack(an explanation of a stack). Everytime you start an activity, it pushes into the Activity Stack. So when Activity A calls Activity B, Activity B is now on top of Activity B, and when you press the back button, it also does a pop in the Activity Stack. So in concept, Activity B is gone. Pressing a Home Button is different from pressing back, it pauses the Activity, therefore it still eats a little of the phone's memory.
Here is a good explanation of how Android Manages Activities.

small conformation on activity life cycle

As i am new to Android i need clarify some doubts in Activity Life cycle.
I have two activities A and B.
I launched first Activity A and i called Activity B from Activity A.
So, Activity A went into onPause() state and onStart() state will start for Activity B.
Now i pressed BACK key in Acitivity B.So, automatically onResume() method of Activity A will start.
Then what is state of Activity B?? onPause() or onStop()?
Again if we press BACK key in Activity A what will happen? It goes to Activity B or close the application?
Then what is state of Activity B?
If you hit the back key then, unless you overrode onBackPressed(), the Activity will be finished (finish()). -> The Activity state will be destroyed (onDestroy()).
Again if we press BACK key in Activity A what will happen?
Activity A will also be finished and the app will close itself (since there is no other Activity in the backstack).
AS we pressing back button Activity B should be destroyed
After that we are again going back from Activity A hence application should close

Android Activity-Lifecycle... how to avoid onDestroy()?

I have an App, which uses an ActicityGroup to have a stack of Activitys. If I start a new Activity, I do this as Child of my ActivityGroup. Lets assume I'm in my starting Activity (1), and I start a new one(2), so here is what's getting called:
(1): onPause()
(2): onCreate(), onStart(), onResume()
until here, everything as aspected. if I press my BackButton the stack is as following:
(2): onPause(), onStop(), onDestroy()
(1): onStop(), onDestroy() [sic]
(1): onCreate(), onStart(), onResume()
I see no reason,first why (1) should perform onStop, and onDestroy(), to reCreate again, and second why onRestart never gets called on (1).
Has anyone a Reason for this behavior? Can I 'cancel' calls to onStop() or onDestroy() somehow?
any idea apreciated
Try using FLAG_ACTIVITY_SINGLE_TOP when starting child activity, like:
Window window = getLocalActivityManager().startActivity(id,
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP));
More info here:
http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_SINGLE_TOP
You can avoid Activity onDestroy method when back button pressed in a very simple way,
Just mention
android:persistent="true"
for your activity, that should prevent your activity getting destroyed, To know more in detail you can visit the below SO post
Prevent activity from being destroyed as long as possible
when you press back button it's default behavior is stop or destroy the current activity you can override the back button. If you press home button then it will use the onPause() that means os will consider that you want to continue with current activity when you again launch same activity but if you press back button it means that you finish your current activity and back to the last activity
Onstop() and onDestroy() will not call untill you did not finish activity. Ondestory() called for releasing of resource that occupied by Activity. IF activity use has been over then it is better to destroy this activity.
Second it will be good for memory management scheme. and if you do not call destroy then it will automatically call when you exit from app
and finally
if you doesnot want to call destroy when you press back button then you can use override method onBackpressed()

Life cycle called when backpress is pressed to navigate back to the previous activity?

What activity state is called when another activity, in the same application, is launched, and then the backpress button is clicked to navigate back to it?
What lifecylce method is called during the proccess of going back to the previous activity?
onPause() is called in Activity A when it launches Activity B. After the back button is called in Activity B, onResume() is called in Activity A.
What lifecylce method is called during the proccess of going back to
the previous activity?
According to the docs: onPause(), onStop(), and possibly onDestroy(). In addition to the lifecycle documentation, you might want to read the docs on Tasks and Back Stack.

Categories

Resources