I\'m trying to use FragmentActivity
with ViewPager
to display dynamic list of Fragments
. There are plenty of examples on how to do a s
It works using rootView.post(). Just define rootView variable in class scope and then inflate it onCreateView(). Finally onLoadFinished() simply use it like this:
rootView.post(new Runnable() {
public void run() {
General.showFragment();
}
});
You can copy FragmentStatePagerAdapter
and change it however you like, or create your own PagerAdapter
from scratch. Or simply don't use a Loader.
I had a very similar problem to this and solved it by using a handler on the Fragment.
I wanted to show an alert dialog on onLoadFinished(), similar to the following
public class MyFragment extends Fragment
implements LoaderManager.LoaderCallbacks<T> {
public void onLoadFinished(Loader<T> loader, T data) {
showDialog();
}
public void showDialog() {
MyAlertDialogFragment fragment = new MyAlertDialogFragment();
fragment.show(getActivity().getSupportFragmentManager(), "dialog");
}
}
But this gave an error
can not perform this action inside of onLoadFinished
The solution was to add a handler to the fragment to take care of showing the dialog
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if(msg.what == MSG_SHOW_DIALOG) {
showDialog();
}
}
};
And then modify onLoadFinished(...) to send a message to the handler instead
public void onLoadFinished(Loader<T> loader, T data) {
handler.sendEmptyMessage(MSG_SHOW_DIALOG);
}