Xamarin中如何获取上一个Page/Activity?
Hey there! Great question—tracking the previous page or activity isn't something that's built right into the framework by default, but there are a few solid, platform-specific ways to make this work. Let's break it down based on what you're working with:
If you're dealing with Android Activities, here are two reliable approaches:
Pass the previous activity name via Intent
This is the simplest and most straightforward method for most cases. When you launch a new Activity from the current one, just add the current activity's name as an extra to the Intent:// In your current Activity (e.g., MainActivity) Intent intent = new Intent(this, NextActivity.class); intent.putExtra("previousActivityName", this.getClass().getSimpleName()); startActivity(intent);Then in the target Activity, retrieve the value:
// In NextActivity String previousActivity = getIntent().getStringExtra("previousActivityName");No special permissions needed, and it works across all Android versions.
Manual navigation stack (for more complex flows)
If you need to track navigation history across multiple screens, create a singleton class to maintain a stack of activity names:public class NavigationHistory { private static NavigationHistory instance; private Stack<String> activityStack = new Stack<>(); private NavigationHistory() {} public static NavigationHistory getInstance() { if (instance == null) { instance = new NavigationHistory(); } return instance; } public void pushActivity(String activityName) { if (!activityStack.contains(activityName)) { activityStack.push(activityName); } } public String getPreviousActivity() { if (activityStack.size() >= 2) { // Pop current to access the one before it, then push back String current = activityStack.pop(); String previous = activityStack.peek(); activityStack.push(current); return previous; } return null; // No previous activity exists } }Call
NavigationHistory.getInstance().pushActivity(this.getClass().getSimpleName())in each Activity'sonResume()method, then usegetPreviousActivity()whenever you need the prior screen's name.
Note: Avoid using ActivityManager to fetch the task stack on Android 11+—the GET_TASKS permission is restricted now, so it won't work reliably for most apps.
For .NET-based UI frameworks using Pages, these methods will do the trick:
Leverage the built-in Navigation Stack
If you're using aNavigationPagefor navigation, the framework already maintains a stack of Pages you can access directly:if (Navigation.NavigationStack.Count >= 2) { var previousPage = Navigation.NavigationStack[Navigation.NavigationStack.Count - 2]; string previousPageName = previousPage.GetType().Name; }Just make sure to check the stack count first to avoid index out-of-range errors (this handles cases where the current page is the first one in the stack).
Custom navigation history stack
If you're not usingNavigationPageor need more control over history tracking, create a static class to manage the stack:public static class PageNavigationHistory { private static readonly Stack<string> _pageStack = new Stack<string>(); public static void TrackPage(string pageName) { // Avoid adding duplicates if the page is reloaded if (_pageStack.Count == 0 || _pageStack.Peek() != pageName) { _pageStack.Push(pageName); } } public static string GetPreviousPageName() { if (_pageStack.Count >= 2) { string currentPage = _pageStack.Pop(); string previousPage = _pageStack.Peek(); _pageStack.Push(currentPage); return previousPage; } return null; } }Call
PageNavigationHistory.TrackPage(this.GetType().Name)in each Page'sOnAppearing()method, then useGetPreviousPageName()to fetch the prior page's name when needed.
内容的提问来源于stack exchange,提问作者Ian Gallegos

