Michael S.
Michael S.

Reputation: 305

FragmentPageAdapter quirks and crash

I am pretty new to android development, and I started with a simple project to learn how to handle most of the basic UI.

I've used ActionBarSherlock and ViewPageIndicator, and overall the simple app I've created is working, but I still have some weird problems. Let me describe:

I think I'm experiencing all of those problems because I went really wrong somewhere in my code. Please help me understand my problems, and if you can, point me to some good practices or places where I cant read and understand what exactly I've done wrong, and how can I do it the right way.

I uploaded the code in question to here: https://code.google.com/p/mich-android-testapp/source/browse/trunk/TestApp/

I appreciate any help from you guys!

Upvotes: 3

Views: 242

Answers (1)

user
user

Reputation: 87064

Your code has several problems which are most likely the root for the problems you listed.

First of all you declare a static fragment(one which is declared directly in the xml layout) in the normal activity(the MainActivity) which you try to replace later in the base class in the OnClickListener. This will not go well as you shouldn't do transactions with static fragments. Instead, you should place a container layout in which to programatically add fragments if you're going to act on them(as a side note you should try to use a generic id for the replacement transaction otherwise if you forget to use the same exact id in the BaseActivity subclasses you'll get in trouble).

The second main problem is that you store the fragments from the ViewPager and assume that those will always be the fragments you'll work with. This will fail after a configuration change when the ViewPager will automatically recreate the fragments.

If you're going to use nested fragments then you have to pass getChildFragmentManager() to the FragmentPagerAdapter from the BasePagerFragment.

Also, don't pass data to fragments with ordinary methods as that data will not survive a configuration change(I'm referring to the setPage() method). Use a Bundle instead.

After you solve this problems see if the errors still appear.

Upvotes: 3

Related Questions