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
Related
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'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().
I am having some problems with an activity of my Android aplication.
I have discovered that my app enters twice in 'onCreateView()' method of this activity when orientation changes, and it is causing me problems. When I click the corresponding button and this activity starts is all ok, but whithin this activity when the orientation changes I have some problems because enters twice, i.e. activity restarts twice ...
... Does anyone know the behaviour of an activity when orientation changes? Why does this happen?
Thanks so much.
Since onCreateView() is not part of the Activity lifecycle, but rather is part of the Fragment, I would assume you have a fragment somewhere in your activity.
Also I would assume you add this fragment manually using FragmentManager and FragmentTransaction. Since Android keeps all the added fragments between orientation changes, it is quite possible that you don't check if your Fragment is already present in this activity, so you add it again which causes additional onCreateView to be called.
If this is the case - you need to check if your savedInstance is null within onCreate. If it is - you add fragment. Otherwise activity is getting restored, so fragment is already there.
A lot of assumptions for the answer, I know. But I'm still mastering my telepathy skills :)
When orientation changes onDestroy() is called, followed by onCreate().
For more : http://developer.android.com/guide/topics/resources/runtime-changes.html
Try to add this in AndroidMainfest.xml in your activity:
android:configChanges="orientation|screenSize|keyboardHidden"
This means not to recreate the activity when orientation changes.
If I launch an android activity with flags ClearTop, and use SetContentView to switch views will these activities still suspend in background and follow the typical workflow with onRestart, onPause, etc.?
All these activities are fired off using startActivity btw...
Any activity follows Activity Lifecycle. If it has clear top flag then the activities above, if any, all get closed (there will be onStop and onDestroy) and that clear top activity becomes the top one because of this. For that activity there will be onStart and onResume
I am making a activity using Tab-Host.
I have two tabs. When I start the tab-Host activity, the tab-Host opens the activity and the life-cycle of the activity is calling but when I changed the tab and again open that previous tab the activity is not getting its callback methods like resume.
I don't think there is any specific reason it should restart. For changing configuration (like rotating the device or sliding out a keyboard) there is a specific trigger because the app needs to deal with the change. But any other process should go according to the Activitvy lifeCycle
When your app goes to the background (looses focus) for any reason you get onPause() called, and when it goes back, your onResume() will be called. This is the same for when you go home and then back to your app, or when you switch activities like that. No new intent or something like that, just going back to the activity.
You should put your code that needs to run in the onResume().
Do what you need to do in the activity in onResume() instead. That will get called everytime, not just the first time it is created.
http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
When you switch from one tab to the other and back, the first tab only gets its onResume method called since it has already had its onCreate called the first time.
You can run the code you like in your onResume method if you want anything specific to happen when it gets focus again.