簡體   English   中英

如何使用我的應用程序中的設備后退按鈕一次返回1個屏幕?

[英]How do I use the device back button in my app to go back 1 screen at a time?

我正在嘗試使用內置的后退按鈕,以便它在Web視圖或應用程序內(無論在何處)以1的速度返回1屏幕,因此,假設我有新聞欄目並單擊它,它將顯示一個新聞頁面。 然后,我單擊搜索並轉到Web視圖,當我單擊返回時,它應顯示我上次打開的新聞部分頁面。 另外,如果我在Web視圖中並在Web視圖中導航並單擊Back,則它應該向我顯示Web視圖中的頁面。 我添加了一些在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;
}

您可以使用此處討論的方法: Android-如何覆蓋“后退”按鈕,這樣就不會完成我的Activity? 如果您想按返回按鈕進行自己的操作。

您還可以提供如下所示的向上導航: http : //developer.android.com/training/implementing-navigation/ancestral.html

我個人比較喜歡第二種選擇

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM