Reputation:
I am reading an android book and I faced an app about fragments with ViewPager. I will list here the 3 classes I have my question about:
The FragmentActivity class:
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
public class ViewPagerFragmentDemoActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ViewPager pager=findViewById(R.id.pager);
pager.setAdapter(new SampleAdapter(getSupportFragmentManager()));
}
}
The PagerAdapter:
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class SampleAdapter extends FragmentPagerAdapter {
public SampleAdapter(FragmentManager mgr) {
super(mgr);
}
@Override
public int getCount() {
return(10);
}
@Override
public Fragment getItem(int position) {
return(EditorFragment.newInstance(position));
}
}
And the Fragment class:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
public class EditorFragment extends Fragment {
private static final String KEY_POSITION="position";
static EditorFragment newInstance(int position) {
EditorFragment frag=new EditorFragment();
Bundle args=new Bundle();
args.putInt(KEY_POSITION, position);
frag.setArguments(args);
return(frag);
}
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View result=inflater.inflate(R.layout.editor, container, false);
EditText editor=result.findViewById(R.id.editor);
int position=getArguments().getInt(KEY_POSITION, -1);
editor.setHint(String.format(getString(R.string.hint), position + 1));
return(result);
}
}
I understand that in the activity we register the SampleAdapter to be the Pager's adapter,then in the adapter every time we want to display a fragment we call getItem() which in turn returns an EditorFragment to display on the screen,and here exactly is my problem in the method that returns this fragment.
Since this method creates an EditorFragment(),adds some arguments to the fragment and return the fragment to getView().So when does onCreateView() gets called and who invokes it?
(I tried using log messages to trace the program but couldn't infer an answer to my ques.).
Edit( to specify my question more because the answers and comments were all things I already know):
"The user launches the app (let's say this app has only this ViewPagerFragmentDemoActivity), this activity's onCreate() is called,which sets the adapter,then for the first screen to appear,the getView() in the adapter class is called, which in turn calls newInstance() method of EditorFragment class, this method creates a fragment (using the constructor obviously) and adds some key-value to it using Bundle object and returns this fragment to the calling method(getView())."
1.Is this trace true?
2.where is the calling of onCreateView() in all this?
3.What happens after the Fragment is returned to getView()?Is it at this time where android framework invokes onCreateView() on this object of Fragment?
Upvotes: 0
Views: 932
Reputation: 54194
After your Activity
has walked through its startup lifecycle (i.e. after onResume()
), it will start drawing to the screen. The exact manner in which Android draws views to the screen is more complicated than it's worth getting into right here, but there will be a measurement pass done to figure out how big everything is.
That measurement pass will call ViewPager.onMeasure()
, which will call ViewPager.populate()
in order to know how big the ViewPager
is.
First, populate()
will call ViewPager.addNewItem()
, which will call into FragmentPagerAdapter.instantiateItem()
. This method does two things: (1) start a FragmentTransaction
and (2) call your adapter's getItem()
method. (This is the answer to your third question: just returning a Fragment from getItem()
doesn't do anything... yet.)
Next, populate()
will call FragmentPagerAdapter.finishUpdate()
. This method will call commitNowAllowingStateLoss()
on the FragmentTransaction
created above. Because it commits "now" (rather than asynchronously), this will start your Fragment.
The FragmentManager
handles executing this FragmentTransaction
. It will eventually call FragmentManager.moveFragmentToExpectedState()
, which will eventually call Fragment.performCreateView()
.
And now we're at the end of the answer. Fragment.performCreateView()
calls your fragment's onCreateView()
method.
Here's an image showing the whole call stack:
Upvotes: 2
Reputation: 778
After the onCreate() is called (in the Fragment), the Fragment's onCreateView() is called by the system to create its view so the whole flow for a fragment would look like this and oncreateview would get called twice during its life cycle
Upvotes: 1
Reputation: 43728
onCreateView
is called by the Android framework when the fragment needs to create its view.
Upvotes: 0