0

I am trying to use the built in back button such that it goes back 1 screen within a webview or within the app( no matter where) by 1, so suppose if i have a news section and I click on it, it will display a news page. Then I click on search and go to a webview and when I click on back it should show the news section page i last opened. Also, if I am in the webview and navigate within the webview and click back then it should show me a page within the webview. I have added some back code which works within a webview. However, it does not work for the enws section page. It takes me back to the news section instead of the specific page I opened last under news section. How do I fix my code to achieve the same? Here's the code I added for back:

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;
}
  • try implementing onbacpressed() method which gets called when you press the device back button – khubaib Sep 19 '13 at 15:31
  • no clue how to override my existing function to implement that –  Sep 19 '13 at 16:19
  • Just implement it like this in your Activity @Override public void onBackPressed() { } – khubaib Sep 19 '13 at 16:31
  • which activity? I am using fragments and I already have a backpressed function there? can you show it with respect to my code and add the function body which helps to take it back by -1 –  Sep 19 '13 at 16:33

1 Answers1

0

You could use the method that was discussed here: Android - How To Override the "Back" button so it doesn't Finish() my Activity? if you want to make your own action happen on pressing the back button.

Also you could provide the up navigation as shown here: http://developer.android.com/training/implementing-navigation/ancestral.html

I personaly would prefer the second option

Community
  • 1
  • 1
Sven Niehus
  • 487
  • 5
  • 24
  • can you explain with respect to my code, that seems a bit confusing for me, new to android sorry. –  Sep 19 '13 at 15:39
  • You just need the steps mentioned in android developer library (http://developer.android.com/training/implementing-navigation/ancestral.html) – Sven Niehus Sep 19 '13 at 15:48
  • That is for navigation and doesnt work in my scenario. I dont have a custom back button but am trying to use the device back button to recognize previous clicks –  Sep 19 '13 at 16:00