Reusing Activity and Fragment objects - android

Is it possible that an Activity or Fragment object can be reused by Android system after onDestroy() is called?
In other words: is every call for onCreate() for Activity object and respectively onAttach() for Fragment object preceded by calling its constructor?

You shouldn't do that. Only framework should call methods like onCreate, onAttach etc..
After onDestroy activity should not be used!
ActvitiyLifecycle
Fragments

Related

Is there any misunderstanding about the onActivityCreated fragment callback?

onActivityCreated seems to mean "This fragment callback is executed just after the activity has been... created... I mean, just after the fragment is correctly attached to the activity. There, you can safely call getActivity, it won't return null, except if the activity is null for some special reason".
However, I've seen that the fragment callback onAttach is called even after OnCreate, it means that the fragment has been attached to the activity, which has been created.
The complete workflow for a fragment (and for a fragment dialog, which is frequently left behind) is: onAttach -> onCreate -> onCreateView -> onActivityCreated. So in each of these 4 methods (perhaps not onAttach I don't know), the activity is normally not null and attached to the fragment.
So my question is: why does the callback onActivityCreated since the activity is, in fact, already created and attached to the fragment 3 callbacks ago???
So basically in onAttach() we get confirmation is that activity is attached to my fragment, I can use getActivity() to fetch things like resources like
getActivity().getResources.getDrawable(R.drawable.abc)
but Suppose if You want to fetch the views inflated in activity's xml like If you want to access
getActivity().findViewById(R.id.Myelement)
You might get null here, so OnActivtyCreated() ensures that activity's view is inflated, You can access activity's views now (Activity's view has been created (onActivityCreated))

How to get last onPause fragment trigger?

I have activity which contain 3 fragment.
when the activity onPaused the 3 fragment set a value to a variable.
I want to check that var in the onPause activity.
but the problem that onPause activity called first and then the fragment onPause called.
How to solve that ?
so i need to run a function when all onPause function finish running ?
thanks
You could define an interface in your fragment and make the activity implement that interface. Then define one method like variableWasSet() in that interface and call it in onPause() in your fragment after you set your variable. That's a recommended way for communication between fragments.
Or use an event bus system for communication between fragments and activities.
Like otto or EventBus.

what is the different between onCreate() and onCreateView() lifecycle methods in Fragment?

I don't know when to use onCreate() or onCreateView().
I have used onCreate() and onCreateView() lifecycle methods.
I think onCreate() for Activity and onCreateView() for Fragment. But I am not sure. Can I use onCreate() LifeCycle method in Fragment? I hope somebody can help me!
onCreate is called on initial creation of the fragment. You do your non graphical initializations here. It finishes even before the layout is inflated and the fragment is visible.
onCreateView is called to inflate the layout of the fragment i.e graphical initialization usually takes place here. It is always called some time after the onCreate method.
Activity lifecycle explained - http://developer.android.com/reference/android/app/Activity.html
Fragment lifecycle explained - http://developer.android.com/guide/components/fragments.html#Creating
Detailed lifecycle diagram - https://github.com/xxv/android-lifecycle
From documents :
onCreate
Called when the activity is starting.
This is where most initialization should go: calling setContentView(int) to inflate the activity's UI, using findViewById(int) to programmatically interact with widgets in the UI, calling managedQuery(android.net.Uri, String[], String, String[], String) to retrieve cursors for data being displayed, etc.
You can call finish() from within this function, in which case onDestroy() will be immediately called without any of the rest of the activity lifecycle (onStart(), onResume(), onPause(), etc) executing.
Derived classes must call through to the super class's implementation of this method. If they do not, an exception will be thrown.
Link to documentation of onCreate
onCreateView
Called to have the fragment instantiate its user interface view. This is optional, and non-graphical fragments can return null (which is the default implementation). This will be called between onCreate(Bundle) and onActivityCreated(Bundle).
If you return a View from here, you will later be called in onDestroyView() when the view is being released.
Link to documentation of onCreateView

onSaveInstanceState() in activity and fragments?

I have an activity which contains 2 fragments. I want to save certain state of the activity and also of the fragments, in order to restore if the activity, or the fragments, are destroyed.
So I'm using onSaveInstanceState both in the fragments and in the activity, and take the data of the bundle passed to onCreate or onCreateView.
This works well besides, when the activity is destroyed. Then in restores it's own data, but, since in onCreate() I instantiate the adapter and the fragments again, they get no state.
How do I solve this?
Thanks in advance.
Most likely the cause of this, is that the Fragment's onCreateView() runs before the Activity's onResume() according to the Fragment lifecycle documentation:
http://developer.android.com/guide/components/fragments.html#Creating

Accessing the Activity from a Fragment

I'm trying to change the activity title from a fragment (in this case, it's an android.support.v4.app.Fragment). To this end, I save the activity in an attribute on the fragment when onAttach() is called on the fragment. According to the docs, onAttach() should be called before onCreateView(), which I'm using to request some data used to fill up the view. When I kick off the thread for the network retrieval, I want to indicate that in the title, so I'm trying to call this.activity.setTitle() from the Fragment. However, that keeps throwing a NullPointerException. What am I missing here?
You can access the Activity in a Fragment using getActivity(). It can be called safely as soon as onActivityCreated() was called on the Fragment. Before that, it might not be there or might not have been fully initialized yet.
If your thread starts before that, just note the fact somewhere in your Fragment and only change the title after onActivityCreated was called.
Nowadays you can call requiredActivity() too that return FragmentActivityobject and if fragment doesn't come from an Activity, method throws a IllegalStateException

Categories

Resources