问题
Short version:
I have a fragment that maintains a ViewPager
for displaying two other fragments, let's call them FragmentOne
and FragmentTwo
. When starting the app FragmentOne
is visible and FragmentTwo
is off-screen, becoming visible only as one swipes the view to the left.
Normally onStart()
and onResume()
get invoked immediately for both fragments as soon as the app gets started.
The problem I have is when FragmentOne
starts a custom Loader
then onResume()
does not get called on FragmentTwo
until it becomes fully visible.
Questions:
Is this a problem with my code or a bug in the Android Support Library? (The problem did not occur with revision 12 of the library, it started with revision 13.)
If it's a bug in revisons 13 and 18, is there a workaround?
Is there something wrong with my custom Loader
?
Long version:
I have built a sample application that demonstrates the problem. I have tried to reduce the code to the bare minimum but it's still a lot so please bear with me.
I have a MainActivity
that loads a MainFragment
which creates a ViewPager
. It is important for my app that the ViewPager is maintained by a Fragment instead of an Activity.
MainFragment
creates a FragmentPagerAdapter
that in turn creates the fragments FragmentOne
and FragmentTwo
.
Let's start with the interesting bit, the two fragments:
FragmentOne
is a ListFragment
that uses a custom Loader
to load the content:
public class FragmentOne extends ListFragment implements LoaderCallbacks<List<String>> {
private ArrayAdapter<String> adapter;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1);
setListAdapter(adapter);
setEmptyText("Empty");
}
@Override
public void onResume() {
super.onResume();
// initializing the loader seems to cause the problem!
getLoaderManager().initLoader(0, null, this);
}
@Override
public Loader<List<String>> onCreateLoader(int id, Bundle args) {
return new MyLoader(getActivity());
}
@Override
public void onLoadFinished(Loader<List<String>> loader, List<String> data) {
adapter.clear();
adapter.addAll(data);
}
@Override
public void onLoaderReset(Loader<List<String>> loader) {
adapter.clear();
}
public static class MyLoader extends AsyncTaskLoader<List<String>> {
public MyLoader(Context context) {
super(context);
}
@Override
protected void onStartLoading() {
forceLoad();
}
@Override
public List<String> loadInBackground() {
return Arrays.asList("- - - - - - - - - - - - - - - - - - - foo",
"- - - - - - - - - - - - - - - - - - - bar",
"- - - - - - - - - - - - - - - - - - - baz");
}
}
}
It is that Loader
that seems to cause the problem. Commenting out the initLoader line makes the fragment life-cycle work as expected again.
FragmentTwo
changes its content based on whether onResume()
has been invoked or not:
public class FragmentTwo extends Fragment {
private TextView text;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
text = new TextView(container.getContext());
text.setText("onCreateView() called");
return text;
}
@Override
public void onResume() {
super.onResume();
Log.i("Fragment2", "onResume() called");
text.setText("onResume() called");
}
}
And here is the boring rest of the code.
MainActivity
:
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Fragment fragment = new MainFragment();
getSupportFragmentManager().beginTransaction().add(R.id.container, fragment).commit();
}
}
Layout activity_main
:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
MainFragment
:
public class MainFragment extends Fragment {
private ViewPager viewPager;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.frag_master, container, false);
viewPager = (ViewPager) layout.findViewById(R.id.view_pager);
return layout;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
viewPager.setAdapter(new MyPagerAdapter(getChildFragmentManager()));
}
private static final class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
@Override
public int getCount() {
return 2;
}
@Override
public Fragment getItem(int position) {
if (position == 0)
return new FragmentOne();
else
return new FragmentTwo();
}
}
}
Layout frag_master
:
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
回答1:
It appears to be a bug in support library. The change below solves the issue.
// FragmentOne.java
@Override
public void onResume() {
super.onResume();
Handler handler = getActivity().getWindow().getDecorView().getHandler();
handler.post(new Runnable() {
@Override public void run() {
// initialize the loader here!
getLoaderManager().initLoader(0, null, FragmentOne.this);
}
});
}
回答2:
Another workaround that worked for me was to use the MainFragment's loader manager:
getParentFragment().getLoaderManager().initLoader(0, null, this);
来源:https://stackoverflow.com/questions/18301979/onresume-not-called-on-viewpager-fragment-when-using-custom-loader