I am new to android,Fragmentbell to fragmenthome back not working
how to fix fragmentbell to fragmenthome private void goToHome() { not working this line backpress see this image any how to fix this erro not working any backpress
public class Fragmentbell extends Fragment {
LinearLayout linearLayout;
private View root_view;
private RecyclerView recyclerView;
private SwipeRefreshLayout swipeRefreshLayout;
private Adapterbell adapterbell;
public static final String EXTRA_OBJC = "key.EXTRA_OBJC";
DbHandler databaseHandler;
private ArrayList<bell> mNotificationList;
private ShimmerFrameLayout lyt_shimmer;
SharedPref sharedPref;
private List<bell> data = new ArrayList<>();
@Nullable
@Override
public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState ) {
root_view = inflater.inflate(R.layout.fragment_bell, container, false);
//Call this method before you set you adapter
//then set your adapter
loadDataFromDatabase();
recyclerView = root_view.findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new StaggeredGridLayoutManager(1, LinearLayoutManager.VERTICAL));
recyclerView.addItemDecoration(new EqualSpacingItemDecoration(0));
recyclerView.setHasFixedSize(true);
//set data and list adapter
adapterbell = new Adapterbell(getActivity(), recyclerView, data);
recyclerView.setAdapter(adapterbell);
recyclerView.addItemDecoration(new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.VERTICAL));
// on item list clicked
adapterbell.setOnItemClickListener(( v, obj, position ) -> {
databaseHandler = new DbHandler(getActivity());
databaseHandler.updateStatus(data.get(position).getId(), true);
((MainActivity) getActivity()).showInterstitialAd();
// Toast.makeText(getActivity(), String.valueOf(data.get(position).gettitle()), Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getActivity(), ActivityNotificationDetail.class);
//
intent.putExtra("id", (data.get(position).getpost_id()));
Log.v("testti", "" + v.getTag() + " apakah sama " + data.get(position).getpost_id());
this.startActivity(intent);
});
//initShimmerLayout();
return root_view;
}
private void loadDataFromDatabase() {
databaseHandler = new DbHandler(getActivity());
data = databaseHandler.getAllbell();
}
public void onBackPressed() {
goToHome();
}
private void goToHome() {
Fragment newFragment = new FragmentHome();
// consider using Java coding conventions (upper first char class names!!!)
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
}
}
backpress fragmentbell to fragmenthome not working how to fix this error
>Solution :
The method onBackPressed() in your fragment is not called from anywhere. It doesn’t work the same as in an activity. In the method on create of your fragment, try something like this:
requireActivity().getOnBackPressedDispatcher().addCallback(this, new OnBackPressedCallback(true) {
@Override
public void handleOnBackPressed() {
goToHome();
}
});
Here are the docs: https://developer.android.com/guide/navigation/navigation-custom-back#implement
However, if you added to the back stack the transaction to show the Fragmentbell then going back should work without having to implement it.