Reputation: 1946
I have this fragment_settings.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearlayout_settings_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textview_my_account"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="My Account" />
<TextView
android:id="@+id/textview_my_account_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="List" />
</LinearLayout>
with following code:
public class SettingsFragment extends Fragment implements Constants, OnClickListener{
public static SettingsFragment newInstance() {
SettingsFragment f = new SettingsFragment();
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
container.setPadding(0, 0, 0, 0);
final View view = inflater.inflate(R.layout.fragment_settings, container,
false);
initViews(view);
return view;
}
private void initViews(View view) {
// TODO Auto-generated method stub
TextView textView = (TextView)view.findViewById(R.id.textview_my_account);
textView.setOnClickListener(this);
TextView textView1 = (TextView)view.findViewById(R.id.textview_my_account_1);
textView1.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
FragmentManager fragmentManager = getChildFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
switch (v.getId()) {
case R.id.textview_my_account:
SettingsContactDetailFragment settingsContactDetailFragment = SettingsContactDetailFragment.newInstance();
settingsContactDetailFragment.setRetainInstance(true);
fragmentTransaction.replace(R.id.linearlayout_settings_fragment, settingsContactDetailFragment);
fragmentTransaction.addToBackStack(FragmentEnum.SETTINGS_CONTACT_DETAIL_FRAGMENT);
fragmentTransaction.commit();
break;
case R.id.textview_my_account_1:
SettingsContactListFragment settingsContactListFragment = SettingsContactListFragment.newInstance();
settingsContactListFragment.setRetainInstance(true);
fragmentTransaction.replace(R.id.linearlayout_settings_fragment, settingsContactListFragment);
fragmentTransaction.addToBackStack(FragmentEnum.SETTINGS_CONTACT_LIST_FRAGMENT);
fragmentTransaction.commit();
break;
}
}
}
i tried this working. But when i use fragmentTransaction.replace then fragment is added with views which are already in fragment_settings.xml
LinearLayout linearLayout = (LinearLayout)view.findViewById(R.id.linearlayout_settings_fragment);
linearLayout.removeAllViews();
but why below not work ?
fragmentTransaction.replace(R.id.linearlayout_settings_fragment, settingsContactListFragment);
Upvotes: 0
Views: 112
Reputation: 17615
FragmentTransaction.replace method removes only the previously added Fragment's view from the container. It does not remove all the views belonging to the container. That's why it gets appended to the other 2 TextViews
.
If you want to replace the TextView
, then they should be part of another Fragment
, which can be added to R.id.linearlayout_settings_fragment
initially and latter replace it with SettingsContactDetailFragment
or SettingsContactListFragment
fragments.
Upvotes: 2