Answer the question
In order to leave comments, you need to log in
How to handle back button click in android fragments?
Here is the code in the main activity
@Override
public void onBackPressed() {
//Обработка нажатия кнопки назад для фрагментов
FragmentManager fm = getSupportFragmentManager();
BackPressedForFragments backPressedForFragments = null;
for (Fragment fragment: fm.getFragments()) {
if (fragment instanceof BackPressedForFragments) {
backPressedForFragments = (BackPressedForFragments) fragment;
break;
}
}
if (backPressedForFragments!=null){
backPressedForFragments.onBackPressed();
}
super.onBackPressed();
}
public interface BackPressedForFragments {
void onBackPressed();
}
public class PreparationFragment extends Fragment implements BackPressedForFragments
@Override
public void onBackPressed() {
//Код для выполнения
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question