I have 3 fragments in another fragment, and I switch between fragments on a button click.
My issue that when I press the back button, the fragment is hidden and I need to press the back button again to exit the fragment.
How to solve this issue?
this is my fragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (rootView != null) {
ViewGroup parent = (ViewGroup) rootView.getParent();
if (parent != null)
parent.removeView(rootView);
}
try {
rootView = inflater.inflate(R.layout.barber_descrption_layout_fragment, container, false);
} catch (InflateException e) {
}
context = getActivity();
linearLayoutMain = (LinearLayout) rootView.findViewById(R.id.mainfrag);
myTextView_appointment = (MyTextView) rootView.findViewById(R.id.text_appi);
myTextView_map = (MyTextView) rootView.findViewById(R.id.textView2);
myTextView_images = (MyTextView) rootView.findViewById(R.id.img);
rate_btn = (LinearLayout) rootView.findViewById(R.id.rate_button);
rate_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ShowRateDialog(context);
}
});
Fragment fragment = new Appointment_Fragment();
Bundle bundle = new Bundle();
fragment.setArguments(bundle);
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.mainfrag, fragment, fragment.getClass().getSimpleName()).addToBackStack(null).commit();
linearLayoutMain.setBackground(getResources().getDrawable(R.drawable.blue_boarder_map));
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayoutMain.setLayoutParams(params);
//////////////////////////////////
myTextView_appointment.setBackground(getResources().getDrawable(R.drawable.appi_color));
myTextView_appointment.setTextColor(getResources().getColor(R.color.white));
/////////////////////////
myTextView_appointment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myTextView_appointment.setBackground(getResources().getDrawable(R.drawable.appi_color));
myTextView_appointment.setTextColor(getResources().getColor(R.color.white));
myTextView_map.setTextColor(getResources().getColor(R.color.blue_color));
myTextView_map.setBackground(getResources().getDrawable(R.drawable.map_color_white));
myTextView_images.setTextColor(getResources().getColor(R.color.blue_color));
myTextView_images.setBackground(getResources().getDrawable(R.drawable.blue__white));
linearLayoutMain.setBackgroundColor(getResources().getColor(R.color.white));
Fragment fragment = new Appointment_Fragment();
Bundle bundle = new Bundle();
fragment.setArguments(bundle);
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.mainfrag, fragment, fragment.getClass().getSimpleName()).addToBackStack(null).commit();
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(0, 0, 0, 0);
linearLayoutMain.setLayoutParams(params);
}
});
myTextView_map.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myTextView_map.setBackground(getResources().getDrawable(R.drawable.map_color));
myTextView_map.setTextColor(getResources().getColor(R.color.white));
myTextView_appointment.setTextColor(getResources().getColor(R.color.blue_color));
myTextView_appointment.setBackground(getResources().getDrawable(R.drawable.apii_color_white));
myTextView_images.setTextColor(getResources().getColor(R.color.blue_color));
myTextView_images.setBackground(getResources().getDrawable(R.drawable.blue__white));
linearLayoutMain.setBackground(getResources().getDrawable(R.drawable.blue_boarder_map));
Fragment fragment = new MapFragment_Barber();
Bundle bundle = new Bundle();
fragment.setArguments(bundle);
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.mainfrag, fragment, fragment.getClass().getSimpleName()).addToBackStack(null).commit();
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(40, 40, 40, 40);
linearLayoutMain.setLayoutParams(params);
}
});
myTextView_images.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myTextView_images.setBackgroundColor(getResources().getColor(R.color.blue_color));
myTextView_images.setTextColor(getResources().getColor(R.color.white));
myTextView_map.setTextColor(getResources().getColor(R.color.blue_color));
myTextView_map.setBackground(getResources().getDrawable(R.drawable.map_color_white));
myTextView_appointment.setTextColor(getResources().getColor(R.color.blue_color));
myTextView_appointment.setBackground(getResources().getDrawable(R.drawable.apii_color_white));
linearLayoutMain.setBackgroundColor(getResources().getColor(R.color.white));
Fragment fragment = new BarberImage();
Bundle bundle = new Bundle();
fragment.setArguments(bundle);
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.mainfrag, fragment, fragment.getClass().getSimpleName()).addToBackStack(null).commit();
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(40, 40, 40, 0);
linearLayoutMain.setLayoutParams(params);
}
});
return rootView;
}
I need so that when I press the back button I exit the fragment at the first click withiut the need of multiple clicks.