1. Overview
When developing Android applications inovlving multiple Fragments and Activities, managing navigation becomes a critical task. This article explores four distinct Fragment navigation scenarios:
- 1. Navigating between Fragments within the same Activity
- 2. Launching a new Activity from a Fragment
- 3. Starting a specific Fargment in a new Activity from another Activity
- 4. Starting a Fragment in a new Activity from another Fragment
2. Setup
2.1 Layout Configuration
In the MainActivity layout, define a container for hosting Fragments:
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
2.2 Creating Fragment Classes
Create a Fragment class that inflates a layout and sets up click listeners:
public class MyFragment extends Fragment {
private View contentView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
if (contentView == null) {
contentView = inflater.inflate(R.layout.my_fragment, container, false);
Button btnNavigate = contentView.findViewById(R.id.my_button);
Button btnBack = contentView.findViewById(R.id.my_return);
Button btnLaunchActivity = contentView.findViewById(R.id.my_other);
btnNavigate.setOnClickListener(this::onButtonClick);
btnBack.setOnClickListener(this::onButtonClick);
btnLaunchActivity.setOnClickListener(this::onButtonClick);
}
return contentView;
}
public void onButtonClick(View view) {
// Navigation logic will be implemented here
}
}
2.3 Initial Fragment Display
Load the initial Fragment into the container in MainActivity:
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, new MyFragment())
.addToBackStack(null)
.commit();
3. Fragment Navigation Scenarios
3.1 Switching Between Fragments in the Same Activity
To navigate from one Fragment to another within the same Activity:
getActivity().getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, new TargetFragment())
.addToBackStack(null)
.commit();
3.2 Launching a New Activity from a Fragment
Standard Activity launch from a Fragment:
Intent intent = new Intent(getActivity(), TargetActivity.class);
startActivity(intent);
3.3 Launching a Specific Fragment in a New Activity
Pass a parameter to the target Activity to display a specific Fragment:
Intent intent = new Intent(OtherActivity.this, MainActivity.class);
intent.putExtra("fragment_id", 1);
startActivity(intent);
In the target Activity:
int fragmentId = getIntent().getIntExtra("fragment_id", 0);
if (fragmentId == 1) {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, new TargetFragment())
.addToBackStack(null)
.commit();
}
3.4 Launching a Fragment in Another Activity from a Fragment
Similar to the previous example, but initiated from a Fragment:
Intent intent = new Intent(getActivity(), TargetActivity.class);
intent.putExtra("fragment_id", 1);
startActivity(intent);