Android Routing Management: Activity, Fragment, and Hybrid Page Navigation
Android routing management separates native navigation—using Activity launch modes, the ActivityManagerService task stack, intents, and the Navigation component for fragments—from hybrid navigation, where Activities host WebView, Weex/React‑Native, or Flutter pages and communicate via intents, bridges, or platform channels to achieve flexible page routing.
In Android development, routing management can be divided into two major stacks: the native page stack (based on Activities and Fragments) and the hybrid page stack (including Activity‑H5(WebView), Activity‑Weex/React‑Native, and Activity‑Flutter).
1. Activity launch modes – Android provides four launch modes (Standard, SingleTop, SingleTask, SingleInstance) that affect how Activities are created and how they behave when the back button is pressed. Standard creates a new instance each time; SingleTop reuses the top instance; SingleTask reuses an existing instance within a task and clears the stack above it; SingleInstance creates a separate task for the Activity. These modes directly influence the back‑stack order and memory consumption.
2. ActivityManagerService (AMS) – AMS manages the lifecycle of Activities through three core objects: ActivityRecord (represents an Activity instance), TaskRecord (the task stack that follows a LIFO order), and ActivityStack (holds the foreground and background TaskRecords). This model enables flexible creation, back navigation, and reuse of pages.
3. Intent as the bridge between Activities
// Explicit Intent: use the class object
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
// Implicit Intent: set category, action, data
Intent intent = new Intent();
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setAction("com.test.action");
startActivity(intent);
new Intent(Intent.ACTION_VIEW, Uri.parse(Url));Explicit intents specify the target component directly and are typically used within the same app. Implicit intents rely on intent filters (action, category, data) to let the system find a suitable component, which is useful for cross‑app navigation.
4. Fragment navigation – Fragments enable flexible UI composition, especially on larger screens. Common navigation patterns include:
① activity1_fragment1 → fragment2
② activity1_fragment1 → activity2
③ activity1_fragment1 → activity2_fragment2
④ activity1 → activity2_fragment2The Android Navigation component encapsulates these patterns, providing a unified back‑stack and safe‑args handling.
5. Hybrid page navigation
5.1 Activity‑H5 (WebView) – An Activity can host a WebView to load H5 pages. To handle back navigation inside the WebView instead of exiting the Activity, developers use WebView APIs:
// WebView navigation APIs
WebView.canGoBack(); // check if can go back
WebView.goBack(); // go back
WebView.canGoForward(); // check if can go forward
WebView.goForward(); // go forward
// Intercept back key to control WebView navigation
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KEYCODE_BACK && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}5.2 Activity‑Weex / React‑Native – These frameworks render native UI via a JavaScript bridge. Page jumps between native and RN are achieved through Intent (native → RN) or Navigation (RN → RN). Native‑to‑RN communication requires exposing a Java module that RN can call via a bridge.
5.3 Activity‑Flutter – Flutter uses a widget‑based routing system. Native pages launch Flutter pages via Intent, while Flutter pages navigate internally using its own Navigator. Interaction between native and Flutter is performed through platform channels.
Overall, Android routing management combines AMS‑based task stack control, Fragment‑based Navigation, and various hybrid bridge mechanisms to provide a highly flexible navigation architecture.
Tencent Music Tech Team
Public account of Tencent Music's development team, focusing on technology sharing and communication.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.