Android Fragment Vs Activity

Why some apps win big in terms of responsiveness ?

Too many Activities
No reusable components

Fragments Vs Activity

Well it comes with experience.

Experts will tell you, “When I see the UI, I will know whether to use an Activity or a Fragment”. 
In the beginning this will not make any sense, but in time, you will actually be able to tell if you need Fragment or not.

There is a good practice I found very helpful for me. It occurred to me while I was developing an App for Pariksha.Co to help students with their placement preparation.

It’s like this, imagine a box which represents a screen. Can you load another screen in this box? If you use a new box, will you have to copy multiple items from the 1st box? 
If the answer is Yes, then you should use Fragments, because the root Activity can hold all duplicated elements to save you time in creating them, and you can simply replace parts of the box.

But don’t forget that you always need a box container (Activity) or your parts will be dispersed. So one box with parts inside.

Take care not to misuse the box. Android UX experts advise when we should explicitly load another Activity, instead to use a Fragment (like when we deal with the Navigation Drawer which has categories).


Well to the code…

Bunch of fragments , how would you handle the Navigation?
BackStack is there to help you!

While adding or replacing the fragments to the fragment container, you need to add them to stack. 
This is how you add fragments to fragment Container.

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// you have to set the tag while adding the fragment
transaction.addToBackStack(tag);
transaction.replace(R.id.parentFragment, fragment,tag).commit();

How to handle the Navigation ?

  • Write this method in the activity where the bottom navigation is added
  • Get the tag of the fragment currently present in this fragment container using the below helper method
  • Check that tag
  • If null, then it’s your Activity Back
  • If not, then you have to pass on that event to the fragment present in the fragment container
public String getActiveFragmentTag() {
if (getSupportFragmentManager().getBackStackEntryCount() == 0) {
return null;
}
String tag = getSupportFragmentManager().getBackStackEntryAt(getSupportFragmentManager().getBackStackEntryCount() — 1).getName();
return tag;
}
public void onBackPressed() {
String currentFragmentTag = getActiveFragmentTag();
Log.d(TAG, “onBackPressed: “+currentFragmentTag);
if(currentFragmentTag == null){
//Activity Back
super.onBackPressed();
}else {
//Pass on this event to the present fragment
if (currentFragmentTag.equals(“Home”)) {
if(video_fragment !=null){
video_fragment.handleVideoFragmentNavigation(); }
} else if (currentFragmentTag.equals(“Car”)) {
test_fragment.handleTestFragmentNavigation();
} else if (currentFragmentTag.equals(“Cycle”)) {
profile_fragment.handleProfileFragmentNavigation();
} else if (currentFragmentTag.equals(“Walk”)) {
profile_fragment.handleProfileFragmentNavigation();
}
}
}

Well in the above gif, you can do it like this. In the MainActivity containing the BottomNav, there is a fragment ParentFragment. Within this ParentFragment , you can add 4 fragments :

Home Fragment

Car Fragment

Cycle Fragment

Walk Fragment

Each time a BottomNav item is selected, you can add that particular fragment to parent fragment like this :

Fragment fragment = new Fragment();
getSupportActionBar().setTitle("Home");
HomeFragment = (HomeFragment) fragment;
String tag = "Home";
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.addToBackStack(tag);
transaction.replace(R.id.parentFragment, fragment,tag).commit();

This is how you handle the navigation in HomeFragment

public void onBackPressed() {
int count = getFragmentManager().getBackStackEntryCount();
if (count == 0) {
activity.onBackPressed();
} else {
getFragmentManager().popBackStack();
}
}

Can you right now look at your UI and figure out if you need an Activity or a Fragment?

Did you get a new perspective?

I think you did.

If you like this article, please consider 💚ing and sharing it!