I have a ViewPager with 4 views. View #3 needs to respond to some broadcasted events. Hence, I register a custom BroadcastReceiver in onCreate as described here. This works if I make sure to show view #3, triggering the onCreate event. Otherwise, it doesn't work, obviously.
How can I make sure that onCreate is indeed called for fragments waiting to be shown. I.e. How to force fragment creation?
See comment above. setOffscreenPageLimit is the key.
Related
I have inherited some code hence I don't have true freedom to change it. :(
I have a main activity, from which other activities (I will refer to these as sub activities from now on) are called. Whenever one of these completes, it calls finish and returns data to the main activity.
Each activity (including the main one) has a bar on the top that displays a custom view. The custom view contains a canvas which has a drawing that is dependant upon the state of the network.. i.e. wifi/mobile etc...
Since that 'state' data never changes, it's held within a singleton and the view gets data from the singleton to define what it draws. That is working with no issues, i.e. the data is always as I expect it.
When I first launch the MainActivity, as the network changes, the data changes and each call to 'invalidate' the view receives a system call to 'onDraw' as I would expect.
In each of the sub activities the same is again true.
Upon finishing a sub activity and returning to the mainActivity, calls to invalidate no longer cause a call to onDraw to occur.
I have looked at this for quite a while now and just cannot figure out what is going wrong.
In my constructor I have:
setWillNotDraw(false);
Whenever the data changes the following methods are called:
invalidate();
requestLayout();
Now, there's one more thing... upon returning to the activity at that immediate point, I refresh and this DOES draw correctly, i.e. invalidate does trigger an onDraw call... any subsequent network changes (which are propogated) fail to result in the onDraw call.
I'm wondering if this is to do with the view somehow being detached. I can see that 'onDetachedFromWindow' is called, however the trigger for this is the destruction of the subactivity, hence I don't see why that should affect the MainActivity but it's the only thing I can think of.
I'm hoping I've provided enough information for someone to help me...
Well, in the end my answer has very little to do with the question and I guess this is an example of how an issue can be solved by going back to absolute basics and checking for the obvious.
My activities all inherit from an abstract activity. Within that activity there is an instance of the view. The views in which I was having trouble were using that declaration as opposed to having their own instance, hence behaviour from one activity was then affecting another inadvertently.
So, if I'd been able to post up all the code, I'm sure someone else would have spotted this but, unfortunately I couldn't in this instance.
Still, whilst this posting doesn't provide a resolution that will help others, maybe it does say... step back and check the obvious first!
It seems that onSaveInstanceState when overridden in a custom View is not called if the activity that created the View also has onSaveInstanceState overridden. Why is this and is there a way to call both? Is one better to use than the other? Since not all Views are custom I find that it might be necessary to have the activity call onSavedInstanceState too.
In addition if you call it in the Custom View, how do you tell the activity to not perform the time consuming tasks, upon rotation, that were used to create the View in onCreate in the first place? The onSaveInstanceState in the View was used to stop the activity from performing time consuming tasks again and again.
You have to add super.onSaveInstanceState() in your overridden method, it calls this method in all attached fragments and views.
It is a good practice to call super.onSaveInstanceState() every time you override it.
I am having a Fragment in which I have used Handler which sends a request after each 60 seconds. This is working fine till I am on the same Fragment.
But when I redirect to other Activity or Fragment in some other Activity, the same Handler is working there also, I want this Handler to work only on that Fragment and want to remove it when I am on some other Activity or Fragment.
I have also used handler.removeCallbacks(runnableCode); on onDetach function of that fragment.
Please help me if you have any idea here, thank you so much in advanced.
You should not call removeCallbacks at onDetach. Your Fragment can be invisible but still not destroyed (paused). notice that onDetach is called after onDestroy. According to the docs:
Called when the fragment is no longer attached to its activity. This
is called after {#link #onDestroy()}.
Use removeCallbacks on onPause instead.
If you want more info then please add your code and we will see why it's not working (but after changing the call to removeCallbacks)
I have several Fragments that are hosted by an activity. They register listeners that are called from a custom Application class in onResume() and unregister them in onPause().
The Activity sometimes exchanges Fragments by using fragmentTransition.replace(...)
Sometimes (quite rare) getActivity() that is called in the listeners returns null.
How is that possible? The listener should not be called because he should unregister first?
To force this, you can install my app and click on a cover (calls replace on a new fragment) and turn the device (runtime change) at the same time.
Thanks for the contribution. I fixed this issue. It was caused by a very rare race condition. I locked the accessors correctly, but assumed something wrong, when I used a handler to post to the ui thread. This had a very tiny delay that caused the race condition to fail.
I fixed it by using getActivity().runOnUiThread(...) instead which has no delay.
Sorry for bothering with this very specific problem.
In part of my Activity's code, I am calling Activity.finish() to close my activity, and the application returns to the main OS "desktop" window.
However, if I click on my application icon again, onCreate does not seem to be called and my view remains the same as when finish was called.
Perhaps, I'm not understanding the lifecycle correctly, but I thought that destroy completely destroyed the activity, and the next time it was invoked it would call onCreate.
Where am I misunderstanding this?
Thanks
In part of my Activity's code, I am calling Activity.finish() to close
my activity, and the application returns to the main OS "desktop"
window.
However, if I click on my application icon again, onCreate does not
seem to be called (...)
Yes, it is called. Just Log.d and you'll see.
and my view remains the same as when finish was
called.
It may remain the same because the XML content and all views instantiated are again created. However, if you modify stuff and layouts in code you'd see that it is restored to the defaults as in setContentView(int layout).
Perhaps, I'm not understanding the lifecycle correctly, but I thought that destroy completely destroyed the activity, and the next time it was invoked it would call onCreate.
As I said, it calls.