I'm trying to give an alert when leaving a fragment by different ways, and user can choose to stay in the current fragment. Is that possible?
Like there is an activity A and two fragment F1 & F2. Now you are in F2, backbutton lead to F1 and some menu item in A also lead to F1, how I write code in one place to stop both of these two events?
onstop is invoked when fragment stopping, how can I stop this stopping proccess in the onstop method?
Depending on what you are trying to achieve, you could override the following methods:
onDestroy
onStop
onPause
onResume
onBackPressed
In addition to the lifecycle methods mentioned by dustedrob, you should also read up on onDestroyView and onDetach to see if those suit your needs.
You can use onBackpressed() of an activity that is attach to all fragment.
Show an alert on Onbackpressed().
Related
I trace lifecycle method's callback from Activity and Fragment components and i have a lot of questions. I need you help to understand some points.
Situation 1:
One launcher Activity with one Fragment, which filled it. When app started, Fragment become active and user see only Fragment.
Lyfecycle:
Activity: onCreate
Activity: onStart
Fragment: onAttach
Fragment: onCreate
Fragment: onCreateView
Fragment: onViewCreated
Fragment: onActivityCreated
Activity: onResume
Fragment: onResume.
Questions:
1) There is no onResume between 2 - 3, because of Activity lose focus?
2) I don't think i understand what happen on 8 - 9. It seems like some kind of "switch focus" between Fragment and Activity. Ok, i can understand: 9 is about Fragment came to the foreground, take focus and onResume was called. But what is 8? When i load Fragment(since 3), Activity is never on foreground.
Situation 2:
Like situation 1, but now Fragment and Activity loaded and i pressed Home:
Activity: onPause
Fragment: onPause
Activity: onStop
Fragment: onStop
Questions:
When i run it, i expect another behavior. I thought Fragment should be the first who called stop methods, like (Fragment onPause -> Fragment onStop - > Activity methods). Why it worked this way?
Situation 3:
Like situation 2, Activity with Fragment are back to the foreground. I remember, they both have Stopped status (because of onStop was called).
Activity: onStart
Activity: onResume
Fragment: onResume
Questions:
I expect 1-2, but i expect that Fragment call onStart, because, if i understand it correct, onStart called when fragment become visible. Why onStart was't called?
Fragment and Activity lifecycles work in parallel. The linear dependence between their lifecycle ends as soon as both activity and fragment are created.
The following figure explains how the two lifecycles are interconnected.
Note that onStart and onResume (and similarly, onStop and onPause) for both fragment and activity execute in parallel and there is no guarantee of order. Sometimes fragment will take precedence over activity, and vice versa.
The only guarantee is that activity's onCreate will always be called first. After that, fragment acts on its own.
I have 2 activities with fragments inside of them. Whenever I press the back button, the fragment gets destroyed (Used FragmentTransaction's replace method for adding them into the activity) instead of going back to the first activity immediately.
How can I achieve the behavior I want to have?
You can override onBackPressed method in your activity and check if the fragmentManager.getBackStackEntryCount()==0. If yes finish() the activity.
Hope it helps.
I have a scenario where I have 2 fragments.
Clicking on a button in the first fragment takes you to the 2nd fragment.
By clicking the "UP" button in the 2nd fragment you'll get navigated back to the first fragment. Unfortunately the OnCreateView() method of the first fragment is not called.
Is there a way to call it? Which methods are called by clicking the "up" button?
It won't be called since 1st fragment is not yet detached from its activity and not destroyed yet. In your case, onResume() callback would be the best place to put your code.
OnCreateView doesnt get called because the fragment A is already created.
Read about the Fragmetn/Activity lifecycle and u will understand that.
OnResume will get called once pressed back from Fragment B, so u can put your logic in that method.
Right now I have 2 activities. When the second activity is running, the first activity onpause is clicked which means I have to unregister all the listeners (which is not what I want when the app is running, I just wanted them unregistered when the app is in the background).
So would it make sense for me to create 2 fragments with one activity. Then I can unregister the listener when the activity onpause is called (only when app goes to background) but it won't be called when fragments are switched.
Is my understanding correct?
Thank you
This totally depends on the activity that you have used to launch your fragment . If two fragments A and B belong to the same activity, then the moment you switch view from Fragment A to Fragment B, your activity does not go to a paused state, means onPause() won't be called, but is still running which is ideally providing the view for Fragment B.
So, you do not need to unregister the listener on change of Fragment in the same activity.
Please correct me if I am wrong.
Yups Activity onPause() would not be called when switching fragments.
I want that my application adds all the fragments when my activity is created ( onCreate) and remove it when my activity die ( onDestroy) but if i try to do this, when i rotate my device my app crash and Log tells me:
java.lang.IllegalStateExceptiom : can not perform this action after onSaveInstanceState
There's a way to do what i wanna do?
Try removing them in onPause before you call super.onPause(). You will then find that the fragments disappear when you rotate the device. So if you then also move the adding of the Fragments from onCreate into onResume you should be ok.
http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle