Ysd i posted one post here but it seems doesn't solve my problem. I also look for similar post in stackoverflow, but it doesn't work for me as well. In this post, i think i have to explain in details of what i want to achieve.
Basically, I have 2 Fragments in 1 activity. From the following screenshot, when user enter sth and click button in Fragment 1, it will reach Fragment 2 .
What i want to do is that in Fragment 2, when user click the Back button at the top left hand corner OR press the device back button, it will back to previous Fragment (Fragment 1).
two.java
public class two extends Fragment{
TextView get_display_input;
ImageView get_back_button;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_two, container, false);
}
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
get_display_input=(TextView)getActivity().findViewById(R.id.display_input);
get_back_button=(ImageView) getActivity().findViewById(R.id.back_button);
Bundle bundle = getArguments();
String get_name = bundle.getString("input_name_value");
get_display_input.setText("You have entered "+get_name);
get_back_button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
FragmentManager manager=getFragmentManager();
FragmentTransaction transaction=manager.beginTransaction();
getFragmentManager().popBackStackImmediate();
transaction.commit();
}
});
}
}
The above coding is Fragment two. When i try to use
getFragmentManager().popBackStackImmediate();
getActivity().getFragmentManager().popBackStack();
It doesn't work.. Please help.