[android-developers] getActivity() returns null

2012-06-03 Thread Gink Labrev
Using the following sample codes, the app crashes when rotates screen and
press the menu button.
The app uses ViewPager + Fragments.

http://pastebin.com/LcR2f2uM - Activity
http://pastebin.com/tUFThnzr - PageAdapter
http://pastebin.com/589zBWLy - Fragment

What's happening ? I found these thread about similar cases, but I didn't
solve the problem yet.

http://stackoverflow.com/questions/9727173/support-fragmentpageradapter-holds-reference-to-old-fragments
http://stackoverflow.com/questions/9039877/android-fragment-screen-rotate
http://stackoverflow.com/questions/7951730/viewpager-and-fragments-whats-the-right-way-to-store-fragments-state

Sorry for English.

Thanks and regards,

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Re: [android-developers] getActivity() returns null

2012-06-07 Thread Gink Labrev
Solution:
http://stackoverflow.com/questions/7998736/how-to-notify-all-fragments-in-viewpager-that-one-of-them-changed


2012/6/3 Gink Labrev 

> Using the following sample codes, the app crashes when rotates screen and
> press the menu button.
> The app uses ViewPager + Fragments.
>
> http://pastebin.com/LcR2f2uM - Activity
> http://pastebin.com/tUFThnzr - PageAdapter
> http://pastebin.com/589zBWLy - Fragment
>
> What's happening ? I found these thread about similar cases, but I didn't
> solve the problem yet.
>
>
> http://stackoverflow.com/questions/9727173/support-fragmentpageradapter-holds-reference-to-old-fragments
> http://stackoverflow.com/questions/9039877/android-fragment-screen-rotate
>
> http://stackoverflow.com/questions/7951730/viewpager-and-fragments-whats-the-right-way-to-store-fragments-state
>
> Sorry for English.
>
> Thanks and regards,
>

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

[android-developers] getActivity returns null after restoring application

2012-08-16 Thread TheNetStriker
I've created an FragmentActivity containing a ViewPager which loads its 
Fragments from an FragmentPagerAdapter. I need to call the Fragments from the 
Activity, so I've created an interface and a way to get a reference to the 
created Fragments. I is working pretty well until the Activity is destroyed and 
recreated. Then the Fragments are not linked to the Activity any more and I get 
null when calling getActivity(). What am I doing wrong? Here is a sample of my 
FragmentPagerAdapter:

private static class MyPagerAdapter extends FragmentPagerAdapter { 
private static Fragment[] fragments;
private String[] fragmentTitles;

public MyPagerAdapter(FragmentManager fm, Context context) {  
 super(fm);
 if (fragments == null) fragments = new Fragment[] { new 
Fragment1(), new Fragment2(), new Fragment3(), new Fragment4() };
 fragmentTitles = new String[] {context.getString(R.string.title_1),

context.getString(R.string.title_2),

context.getString(R.string.title_3),

context.getString(R.string.title_4)};
}  

@Override  
public Fragment getItem(int index) {  
return fragments[index];
}

@Override
public CharSequence getPageTitle(int index) {
return fragmentTitles[index];
}

@Override  
public int getCount() {  
 return fragmentTitles.length;  
}
public Fragment1 getFragment1() {
return (Fragment1) fragments[0];
}

public Fragment2 getFragment2() {
return (Fragment2) fragments[1];
}

public Fragment3 getFragment3() {
return (Fragment3) fragments[2];
}

public Fragment4 getFragment4() {
return (Fragment4) fragments[3];
}  
   }

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en