last day (15 days later) » 

12:48
0
Q: Android: PagerAdapter creating double Fragments after config change

Karl MorrisonI am currently using a custom PagerAdapter (note not a FragmentPagerAdapter). My problem? When I titl the smartphone for screen rotation the Activity as we all know gets destroyed then is recreated. In this notion the Fragments are stored. My problem is that I am not using a PagerAdapter that re...

I don't know why do you store some of these fragments in the activity. Why don't you get the instances of your fragments from the adapter in the activity as well? Or on instantiateItem() you can update the fragment references in the activity.
Where do I get the instances from, the Bundle on onCreate?
You can update your references from instantiateItem().
I don't quite understand, Everytime I rotate the device the instantiateItem creates new Fragments. How do I reference the Fragments that the Activity stores on a config change?
On your instantiateItem() you can call YourActivity.this.myFragment = newFragmentInstance;
12:48
That's creating a new Fragment though (... = newFragmentInstance)?
no, just get the previous reference of a created fragment of yours.
like, in your switch, case 0:, set YourActivity.this.yourLeftFragment = leftFragment;
Hello there!
hello!
Just like ot say thank you for taking the time to help me!
no worries. I hope I can help.
Did you get what I mean?
For example, in your code:
case 0:
itemView = inflater.inflate(R.layout.left, container, false);

leftFragment = new LeftFragment();
ft = fm.beginTransaction();
ft.replace(R.id.left, leftFragment);
ft.commit();
12:51
Nja not really heh, thing is. I've got a PagerAdapter which returns a inflate
you can update the reference to your leftFragment in your activity.
Ah yes! I do do this. I have a interface
public static interface OnLeftFragmentCompleteListener {
public abstract void onLeftFragmentComplete(LeftFragment leftFragment);
}
so below the ft.commit(); just write YourActivity.this.yourFragmentVariable = leftFragment;
When the fragment is completed I give the reference to the Activity
I'll try this instead.
where is the call to the interface in the instantiateItem() method?
You can keep the interface, just make sure to call it from instantiateItem().
12:54
It's being called in the LeftFragment.java here:
@Override
public void onStart() {
super.onStart();
fragmentListener.onLeftFragmentComplete(this);
} @Override
public void onStart() {
super.onStart();
fragmentListener.onLeftFragmentComplete(this);
}
(double paste)
When onStart is invoked, it gives a reference to itself to the activity
However this all works, it's when I rotate the screen. There are 2 lifecycles of LeftFragment
I'll give you some logs :)
This is what happens when I rotate the screen:
11-17 13:56:31.630: D/Karl(27052): LeftFragment.onCreate() LeftFragment{4270f838 #0 id=0x7f050008}
11-17 13:56:31.646: D/Karl(27052): Activity.onCreate() LeftFragment{4270f838 #0 id=0x7f050008}
11-17 13:56:31.646: D/Karl(27052): ------ virewpager is null
11-17 13:56:31.646: D/Karl(27052): Activity.onLeftFragmentComplete() LeftFragment{4270f838 #0 id=0x7f050008}
11-17 13:56:32.068: D/Karl(27052): LeftFragment.onCreate() LeftFragment{4271d190 #0 id=0x7f050008}
11-17 13:56:32.076: D/Karl(27052): Activity.onLeftFragmentComplete() LeftFragment{4271d190 #0 id=0x7f050008}
hmmm
Second last log shows another LeftFragment being created
I have no idea why
I'm guessing it's the Activity as it stores fragments. If I was using the FragmentPagerAdapter this would of worked out dandy, as the FragmentManager is smart enough to "recycle"
My guess is that I'm not recyckling my fragments
Yep. I was writing the same thing.
I am using a custom pagerAdapter which returns a view(inflated layout) and not a fragment, also my fragments are not static. I'm stuck at how I am to "recycle" my fragments
(not static being in the xml file I use a FrameLayout, then replace using FragmentManager)
13:01
I'm looking at the source of FragmentPagerAdapter now
It does this as follows:
If you need any code I can post happily
Fragment fragment = mFragmentManager.findFragmentByTag(name);
if (fragment != null) {
mCurTransaction.attach(fragment);
} else {
fragment = getItem(position);
mCurTransaction.add(container.getId(), fragment,
makeFragmentName(container.getId(), itemId));
}
maybe you can try something similar? It's using the transaction attach() method instead of the replace()? You would to have a holder to make sure which fragments have been created already.
Ahh ok can try, where in my code shall I post this? The instantiateItem()?
Look at this: stackoverflow.com/a/17629575/362298 it's for a FragmentPagerAdapter but you can try to adapt the idea.
yes, in instantiateItem()
Ok I shall have a look and try! :D I'll get back to you asap
13:57
Ok been playing about, I know the "problem"
It has nothing to do with screen rotation
I have 4 pages, When I am at the 3rd page, my pageradapter/viewpager destroys the view of the first page, however the Fragment it is still in the FragmentManager. When I swipe to the second page, THATS when the first pages fragment gets destroyed and a new one is instiated
I did what you said, however when I replace the fragment it's blank, the view is not inflated anymore I'm guessing
Maybe you can somehow remove the reference from the fragmentmanager when the fragment gets destroyed?
like with a listener back to the adapter.
But that doesn't explain why a fragment gets its onCreate() called twice.
14:24
I have a strong feeling I'm missing something in my Pageradapter. When I said "destroys the view" I didn't mean it calls the onDestroy(), I guess the viewPager just removes it
when I try mCurtransaction.add... it says that a fragment is already added
I tried this
ft = fm.beginTransaction();
LeftFragment fragment = (LeftFragment) getSupportFragmentManager().findFragmentByTag("LEFTTAG");

if(fragment != null) {
ft.add(R.id.left, fragment, "LEFTTAG");
} else {
leftFragment = new LeftFragment();
ft.replace(R.id.left, leftFragment, "LEFTTAG");
}
ft.commit();
crahses stating that R.id.left already as a fragment attached to it (which would explain when replacing with a new Fragment it calls the destroy on the current one)
Just checking. you don't have retainInstance(true) on your fragments, right?
no I do not.
That also causes a crash, however a different problem
ok. but if fragment == null, then why replace says it exists? it should have been returned by findFragmentByTag.
I'm sorry, I'm running out of ideas.
nono it was liek this
if(fragment != null) {
ft.add(R.id.left, fragment, "LEFTTAG"); <--- here it says a fragment is already attached
} else {
leftFragment = new LeftFragment();
ft.replace(R.id.left, leftFragment, "LEFTTAG");
}
ah! but you're doing the opposite of the code from FragmentPagerAdapter.
you should add if it's null
and attach if it's not null
14:32
Ok let's try
Ok, no error, but the screen is now blank
I'm returning a View
sorry, I don't know what else to suggest.
is there a way to "reinflate"
you can't really use FragmentPagerAdapter?
well, you could keep the view in a holder as well
and return it from the holder
14:38
I've tried for god and country with FragmentPagerAdapter, I really would want to use it
however it doesnät allow Fragments infragments
doesn't
ah
ok
I have a very specific layout, which is to be very customisable
for example
3 fragments in a viewpager page
or 2, or 4
works very well, I just get this problem hee
hehe
but that's not a problem with fragmentPagerAdapter
i have something like that as well
just not with fragments inside another
the only reason I'm not using fragmentPageradapter is because I can't do this for example:
<fragment>
oops hold on
<fragment>
<fragment />
<fragment />
</fragment>
You mean:
<fragment>
<fragment />
<fragment>
</fragment>
?
14:42
the two nested fragments are for me to decide
depending on layout
the parent fragment is the fragment viewPager wants
in FragmentPageradapter
got it
but what is the error?
or what doesn't work?
inflate exception if I remember correctly
I'm curious now
14:45
I'll do a little test app with FragmentPageradapter :)
I'll get the exception for you
if you can send me a mock code i can try to test it for you as well for a bit
can't promise anything though. :)
will do!
No problem ,you've been a massive hlep :)
let's see if we can get this working. It might be worthy knowing for future reference as well. :)
14:47
I want a highly customizable layout that includes viewpager swiping :)
Thats the whole goal
I've got it to work, except for the fragments firing twice
(hence my stakcoverflow question :P)
Ahhh now I remember the problem
with FragmentPageradapter
it wants you to return a Fragment
Can one return a fragment located in a layout?
(xml file that is)
cause if you can then it will work
I never tried but I don't see why not.
I'
ll try it on the mockup
15:34
here
public Fragment getItem(int position) {
Fragment fragment = new Fragment();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);


switch(position){
case 0:
// this is the trouble
ViewGroup itemView = (ViewGroup) inflater.inflate(R.layout.left_middle, (ViewGroup) findViewById(R.id.pager_large), false);
fragmen t= ????
break;
case 1:
fragment=MiddleFragment.newInstance();
break;
}
return fragment;
}
and heres the xml file
<?xml version="1.0" encoding="utf-8"?>
<Fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/left_middle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="#ff0000" >

<Fragment
class="com.example.testappz.LeftFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/left" />

<Fragment
class="com.example.testappz.MiddleFragment"
The first code is from the FragmentPagerAdapter
You see the line "fragment = ????", I have no idea how to get the left_middle fragment to return there
-------------------
I tried this
FragmentManager fm = getSupportFragmentManager();
ViewGroup itemView = (ViewGroup) inflater.inflate(R.layout.left_middle, (ViewGroup) findViewById(R.id.pager_large), false);
fragment = fm.findFragmentById(R.id.left_middle);
didn't work
---
oh also this is the pager xml file

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/pager_large"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >



</android.support.v4.view.ViewPager>
16:24
hey
just give me some time. I was away and just came back.
16:47
It would be more helpful if you could send me the full source so I can add to a project here and debug. I will try to put something together if you can't reply now.
the full source of the mock above, I mean.
 
1 hour later…
17:58
Is this what you wanted?
Note that I used FragmentStatePagerAdapter instead since, as I understood, you will be adding more fragments later.
Let me know if this works. I will wrap this up as an answer to your question.

last day (15 days later) »