I am trying to use Otto for achieving the following use case.
I have a Viewpager with Fragments that contains a RecyclerView
each. The recyclerviews show posts that the users post to the app. A new post button is attached to each of these Fragments. Once the post Button
is clicked, the user is taken to a new activity and he posts the content there. When he comes back to the ViewPager
fragment screen again, I want the fragment screen to refresh and show the updated contents. I use otto for this.
I am able to make this work when only one fragment is present.
public class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
BusProvider.getInstance().register(this);
}
I have the register method called in the onCreatemethod of the PlaceholderFrament. so obviously when multiple fragments are loaded multiple times, I get the following error. How do I handle this?
Process: com.four.xxxx.xx, PID: 9934
java.lang.IllegalArgumentException: Object already registered.
at com.squareup.otto.Bus.register(Bus.java:222)
at com.four.xxxx.xx.PlaceholderFragment.onCreateView(PlaceholderFragment.java:94)
at android.app.Fragment.performCreateView(Fragment.java:2053)
Edit 1 : PagerAdapter Class as requested :
public class FourScreenActivity extends Activity {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
static final int POST_DEPT_REQUEST = 1;
static final int POST_COLLEGE_REQUEST = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_four_screen);
mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void postInSection1(View view) {
Intent intent = new Intent(this, PostInSection1.class);
startActivity(intent);
}
public void postInSection2(View view) {
Intent intent = new Intent(this, PostInSection2.class);
startActivity(intent);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return PlaceholderFragment.newInstance(position + 1);
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
}
return null;
}
}
}