Reputation: 5531
I'm trying to create a dual pane/ single pane setup for an application. It was working fine a while ago before I put all the inards into the fragments, and I don't know what caused it not to work. The problem is happening when I'm doing an orientation change. At first, it was not recreating the view for some reason. It would call onCreateView, but I was not able to get a handle on the listview inside of the view and was getting null (I know I have the layouts correct for landscape and portrait, as it works in both portrait and landscape if it starts there). So, what I did is a add setRetainInstance to true, thinking that would fix my problem. Well, that brought up a whole other issue of now I'm getting No View Found For Id.
What I think is happening now is that its trying to reattach itself to the ID it had before the orientation change, and it doesn't exist as were on a different layout now. I tried just creating a new fragment and adding it, but that doesn't work either. I'm borderline pulling my hair out trying to work with Android's have thought fragment system that almost impossible to work with. Any help would be appreciated. Here is the relevant code
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.study_guide);
//Create The Ad Service
//Check to see if the view is landscape or portrait
if (this.findViewById(R.id.singlePaneStudyGuide) != null) {
mDualPane = false;
} else if (this.findViewById(R.id.doublePaneStudyGuide) != null) {
mDualPane = true;
}
LinearLayout layout = (LinearLayout)findViewById(R.id.addbox);
adView = new AdView(this,AdSize.SMART_BANNER,"a1511f0352b42bb");
layout.addView(adView);
AdRequest r = new AdRequest();
r.setTesting(true);
adView.loadAd(r);
//Inflate Fragments depending on which is selected
//if (savedInstanceState == null) {
FragmentManager fm = this.getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
SGListFragment newFrag = new SGListFragment();
if (mDualPane == true) {
ft.add(R.id.sgScrollContainer, newFrag, "SgListView").commit();
} else {
ft.add(R.id.singlePaneStudyGuide, newFrag, "SgListView").commit();
}
fm.executePendingTransactions();
//}
}
Ive tried looking for the fragment with the fragment manager and reassigning it to a different layout but for some reason its still looking for the old one.
Upvotes: 3
Views: 3431
Reputation: 1808
You have to rewrite your code as follow:
//Inflate Fragments depending on which is selected
//if (savedInstanceState == null) {
FragmentManager fm = this.getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
// here the trick starts
int oldId = (mDualPane == true) ? R.id.singlePaneStudyGuide
: R.id.sgScrollContainer;
Fragment oldFragment = fm.findFragmentById(oldId);
if(null != oldFragment){
ft.remove(oldFragment);
ft.commit();
fm.executePendingTransactions();
ft = fragmentManager.beginTransaction();
}
// end of tricks
SGListFragment newFrag = new SGListFragment();
if (mDualPane == true) {
ft.add(R.id.sgScrollContainer, newFrag, "SgListView").commit();
} else {
ft.add(R.id.singlePaneStudyGuide, newFrag, "SgListView").commit();
}
Upvotes: 2