我在另一個片段中有3個片段,並且我在按鈕單擊之間切換片段。
我的問題是,當我按下後退按鈕,片段被隱藏,我需要再次按下後退按鈕退出片段。
如何解決這個問題?onBackPressed()在一個片段中
這是我的片段
@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;
}
我需要這樣,當我按下後退按鈕我在第一次點擊withiut需要多次點擊的退出片段。
同樣的問題!它不適合我! –
讓你的父母'Frgament'成爲'BackableFragment',你的3個孩子片段的容器就放在這個'BackableFragment'上。 –
假設你有多個片段。在所有片段中調用Activity.onBackPressed都會導致在任何片段中按Back鍵時活動結束。我建議在片段中設置一個OnBackPressedListener,並讓Activity在特定片段報告OnBackPressed時決定要做什麼。 – Christine