Suppose I have a MainActivity with FragmentContainerView. This FragmentContainerView's size matches the MainActivity.
Then I want to show one fragment at a time in this FragmentContainerView.
Fragment1, Fragment2, and Fragment3.
Each fragment will have one button. When button is pressed, next fragment is loaded.
Fragment1 (Press button)--> Fragment2 (Press button)--> Fragment3 (Press button)--> Fragment1 --> and so on.
So far, I was able to implement, but I am not sure how to make my fragments such that, when I press the back button,
Fragment1 should exit app
Fragment2 should load Fragment1
Fragment3 should load Fragment2
Here is my MainActivity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FirstFragment fragment = new FirstFragment();
Bundle bundle = new Bundle();
bundle.putString("CLASS_NAME", " ");
fragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container_view, fragment, null)
.commit();
}
}
Here is my Fragment1
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
binding = FragmentFirstBinding.bind(getView());
String previous_class = getArguments().getString("CLASS_NAME");
if(!previous_class.equals(" ")){
binding.textViewFirstFragment.setText(previous_class);
}
//On button click, navigate to MainActivity
binding.buttonFirstFragment.setOnClickListener(this);
}
@Override
public void onClick(View v) {
//Log.i("BACKSTACK", "debug: current status of fragment back stack " + getFragmentManager().getBackStackEntryCount());
Fragment fragment = new SecondFragment();
putBundleArgument(fragment);
replaceFragment(fragment);
debug();
}
public void replaceFragment(Fragment fragment) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container_view, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
Here is my Fragment2
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
binding = FragmentSecondBinding.bind(getView());
String previous_class = getArguments().getString("CLASS_NAME");
binding.textViewSecondFragment.setText(previous_class);
//On button click, navigate to MainActivity
binding.buttonSecondFragment.setOnClickListener(this);
}
@Override
public void onClick(View v) {
//Log.i("BACKSTACK", "debug: current status of fragment back stack " + getFragmentManager().getBackStackEntryCount());
Fragment fragment = new ThirdFragment();
putBundleArgument(fragment);
replaceFragment(fragment);
debug();
}
public void replaceFragment(Fragment fragment) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container_view, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
Lastly, here is my Fragment3
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
binding = FragmentThirdBinding.bind(getView());
String previous_class = getArguments().getString("CLASS_NAME");
binding.textViewThirdFragment.setText(previous_class);
//On button click, navigate to MainActivity
binding.buttonThirdFragment.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Fragment fragment = new FirstFragment();
putBundleArgument(fragment);
replaceFragment(fragment);
debug();
}
public void replaceFragment(Fragment fragment) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
/*
ThirdFragment fragment3 = new ThirdFragment();
transaction.remove(fragment3);
SecondFragment fragment2 = new SecondFragment();
transaction.remove(fragment2);
*/
getFragmentManager().popBackStack();
getFragmentManager().popBackStack();
getFragmentManager().popBackStack();
transaction.replace(R.id.fragment_container_view, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
@Override // override this in MainActivity
public void onBackPressed(){
int count = getSupportFragmentManager().getBackStackEntryCount();
if(count == 1)//When there is only 1 fragment left in the stack ie Fragment 1.
finsh();
else
getSupportFragmentManager().popBackStack(); //Fragment 2 and 3 are popped here.
super.onBackPressed();
}