When I start my application, my first Fragment is launched. In that Fragment, I click a button, which replaces the original Fragment with a new one. When I click back on this 2nd Fragment, it does not go back to the first one but rather closes the application and returns to the home screen of my phone. Does anybody know why?
Here is the original Fragment that is launched in my Activity's onCreate Method:
public class Main extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LoginFragment firstFragment = new LoginFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, firstFragment);
ft.addToBackStack(null);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();
}
}}
Here is the code for this Fragment, which contains the button that brings up the 2nd Fragment:
public class LoginFragment extends Fragment implements View.OnClickListener {
private FragmentTransaction ft;
private Button registerButton;
public LoginFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_login, container, false);
registerButton = (Button)view.findViewById(R.id.register_button);
registerButton.setOnClickListener(this);
return view;
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.register_button:{
RegisterFragment registerFragment = new RegisterFragment();
ft = getFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, registerFragment);
ft.addToBackStack(null);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();
break;
}
}
}}
The 2nd Fragment that is launched doesn't contain anything right now. Regardless, when I press back on my phone when Im at the 2nd Fragment, it does not return to my first one.