我正在尝试使用内置的后退按钮,以便它在 web 视图或应用程序(无论在哪里)中返回 1 个屏幕(无论在哪里),所以假设如果我有一个新闻部分并且我点击它,它将显示一个新闻页面。然后我点击搜索并进入一个网络视图,当我点击返回时,它应该会显示我上次打开的新闻部分页面。此外,如果我在 webview 中并在 webview 中导航并单击返回,那么它应该在 webview 中显示一个页面。我添加了一些在 webview 中工作的返回代码。但是,它不适用于 enws 部分页面。它带我回到新闻部分,而不是我上次在新闻部分下打开的特定页面。如何修复我的代码以实现相同的目标?这是我添加的代码:
private boolean goingBack = false;
private boolean onBackPressClearStack = true;
public void setOnBackPressClearStack(boolean b){
onBackPressClearStack = b;
}
public boolean webViewSteppedBack() {
if (mWebview != null && mWebview.canGoBack()) {
mWebview.goBack();
return true;
}
return false;
}
@Override
public boolean backPressed(final MainActivity mainActivity) {
if (webViewSteppedBack()) {
return true;
}
if (onBackPressClearStack) {
goingBack = true;
FragmentUtils.onBackPressedKnockFragsOffStack(mainActivity, this);
}
return false;
}
public static MyWebViewFragment newInstanceNoBackPressed(final FragmentManager manager, final String searchTerm, final String symbolType, int containerViewId) {
MyWebViewFragment fragment = __newInstance(new MyWebViewFragment(), manager, searchTerm, symbolType, containerViewId);
fragment.setOnBackPressClearStack(false);
return fragment;
}
public static MyWebViewFragment newInstanceNoBackPressed(final MyWebViewFragment fragment, final FragmentManager manager, final String searchTerm, final String symbolType, int containerViewId) {
fragment.setOnBackPressClearStack(false);
return __newInstance(fragment, manager, searchTerm, symbolType, containerViewId);
}
@Override
public void onResume() {
super.onResume();
final MainActivity activity = (MainActivity) getActivity();
activity.updateActionBarTitle();
activity.setBackPressListener(this);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (goingBack) {
return null;
}
final MainActivity activity = (MainActivity) getActivity();
activity.setBackPressListener(this);
View view = inflater.inflate(R.layout.fragment_search_answers, container, false);
my webview code.....
return view;
}