Manuel Castro
Manuel Castro

Reputation: 1723

WebAppInterface in WebView does not call onBackPressed

I've a fragment with a WebView inside. I added a custom WebAppInterface that has this method

@JavascriptInterface
public void submitCorrect() {
   Log.d("SUBMIT", "submit");
   getActivity().onBackPressed();
   //getActivity().getFragmentManager().popBackStack();
}

Debugging i saw that the method is called but, I don't know why, neither onBackPressed or popBackStack are working. Any advice?

Upvotes: 2

Views: 517

Answers (2)

Emin
Emin

Reputation: 56

The problem is, calling the onBackPressed method on WebView Thread, You should call it in the main thread. That's the reason it's not working.

Try changing the code as below:

getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                getActivity().onBackPressed();
            }
        });

Upvotes: 3

Doron Yakovlev Golani
Doron Yakovlev Golani

Reputation: 5480

Perhaps finishing the activity instead will work:

@JavascriptInterface
public void submitCorrect() {
   Log.d("SUBMIT", "submit");
   Activity activity = getActivity();
   if (activity != null) 
       activity.finish();
}

Upvotes: 1

Related Questions