So I wish to hide a fragment while showing another with the push of a button. Because of the complexities of my app, I can't use a tabHost here, so I made two buttons that will act as tabs that will show and hide two fragments. One of the buttons works, but the other button hides the current fragment, but fails to show the other. Sometime this button waits a second or so and then shows part of the fragment that was just hidden. Any idea on what's going on?
Here is the code in my xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/top_buttons"
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal">
<TextView
android:id="@+id/text_personal_A"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:onClick="true"
android:text="@string/my_account"/>
<TextView
android:id="@+id/text_personal_B"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:onClick="true"
android:text="@string/my_messages"/>
</LinearLayout>
<FrameLayout
android:id="@+id/dynamic_fragment_A"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<FrameLayout
android:id="@+id/dynamic_fragment_B"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
Here is the code in my Fragment
package com.kofsoft.mobile.fragment;
import com.kofsoft.mobile.R;
import com.kofsoft.mobile.constants.Constants;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
public class PersonalCenterFragment extends Fragment implements OnClickListener{
private TextView accountTab, employeeTab;
AccountInformationFragment accountInfo;
EmployeeInformationFragment employeeInfo;
FragmentManager fragMan;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_personal, container, false);
accountInfo = new AccountInformationFragment();
employeeInfo = new EmployeeInformationFragment();
accountTab = (TextView) v.findViewById(R.id.text_personal_A);
accountTab.setOnClickListener(this);
employeeTab = (TextView) v.findViewById(R.id.text_personal_B);
employeeTab.setOnClickListener(this);
fragMan = getFragmentManager();
FragmentTransaction fragTrans = fragMan.beginTransaction();
fragTrans.replace(R.id.dynamic_fragment_A, accountInfo,
Constants.ACCOUNT_INFO_TAG);
fragTrans.replace(R.id.dynamic_fragment_B, employeeInfo,
Constants.EMPLOYEE_INFO_TAG);
fragTrans.hide(employeeInfo);
fragTrans.commit();
return v;
}
@Override
public void onClick(View tab) {
FragmentTransaction fragTrans = fragMan.beginTransaction();
if(tab.getId() == R.id.text_personal_A){
fragTrans.hide(employeeInfo);
fragTrans.show(accountInfo);
}else if(tab.getId() == R.id.text_personal_B){
fragTrans.hide(accountInfo);
fragTrans.show(employeeInfo);
}
fragTrans.commit();
}
}
Maybe this has something to do with the fact that I'm using two FrameLayouts in my XML to store my fragments. I'm not sure, but wasn't able to find anyone else with this particular problem.