2

I have a strange problem with one Fragment of my ViewPager. I have a ViewPager with three Fragment. Everything was running ok till now. when app starts, the ViewPager (with FragmentPagerAdapter) attached the three fragments so I could navigate swiping from one to another without any problem. the problem came when I tried to restart the app manually (after logging out) like that:

Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage( getBaseContext().getPackageName() );
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);

Now when I logout and app restart application crash and through an IllegalStateException Fragment not attached to Activity when trying to access to resources from on of the fragments.

Here is the code where I attach fragments, the adapter:

public class AppSectionsPagerAdapter extends FragmentPagerAdapter {

        public AppSectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public BaseListFragment getItem(int index) {

            switch (index) {
            case 0:
            case R.id.index_explore:


                BaseListFragment fragment0 = new Fragment0();
                fragment0.setUserVisibleHint(true);

                return fragment0;

            case 1:
            case R.id.index_main:

                if (fragment instanceof FragmentList)
                    ((FragmentList) fragment).removeMap();
                return new Fragment1();

            case 2:
            case R.id.index_my_stuff:

                BaseListFragment fragment2 = new Fragment2();
                fragment2.setUserVisibleHint(true);
                return fragment2;

            default:

                if (fragment instanceof FragmentList)
                    ((FragmentList) fragment).removeMap();

                return new Fragment1();

            }

        }

        @Override
        public int getCount() {
            return 3;
        }

    }

Then, I set the adpater:

mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());

mViewPager.setAdapter(mAppSectionsPagerAdapter);

It is Fragment0 which cause the crash. I don't understand why it happens, since it should re-build everything (so the adapter with fragments too) like it does when application starts for the first time. Maybe there is something that I am assuming that is not correct? I have previously search for some similar question, and there are a lot related with this exception, but don't resolve my case, which I think is a particular one.

tshepang
  • 10,772
  • 21
  • 84
  • 127
DroidBeginner
  • 135
  • 1
  • 3
  • 11
  • Post the activity code where you have added the fragments – Spring Breaker Feb 27 '14 at 09:29
  • I've just edited my question. Hope it helps. Thanks. I was thinking that the problem was with restarting, since it works perfectly till this moment. Maybe the way I restart the app doesn't re-create the activity so, it doesn't attach the fragment but use a saved instance that has already detach them? I don't know if I'm explaining it well. – DroidBeginner Feb 27 '14 at 09:44
  • http://stackoverflow.com/a/10941410/1865860 maybe is useful – dentex Feb 27 '14 at 09:47
  • @dentex, I already saw it, but I don't know how it could help me. Thanks anyway – DroidBeginner Feb 27 '14 at 10:20

1 Answers1

1

I think that I already solved. As I supposed, my problem was the way I was "restarting" the app. It seems like it didn't re-created MainActivity again, so it didn't re-build and attach all fragments. Now I restart in another way, a solution that I found here

Is that correct to use Alarmmanager for this purpose? This is the code I used:

PendingIntent intent = PendingIntent.getActivity(this.getBaseContext(), 0, new Intent(getIntent()), getIntent().getFlags());
AlarmManager manager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
manager.set(AlarmManager.RTC, System.currentTimeMillis(), intent);
System.exit(2);
Community
  • 1
  • 1
DroidBeginner
  • 135
  • 1
  • 3
  • 11