0

I developed an android app which has a list view as its main gui, when the user click the item in the list, a webview will show basing on the content of the item.

It works fine, however when I clicked the back button in the android phone (or simulator), a blank white screen is shown, and the list view appears only after I click the back button again.

Can anyone help explain and help solve the problem?

Here is my WebviewActivity :

public class WebViewActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.active_item_detail);

        if (savedInstanceState == null) {
            WebViewFragment wvf = new WebViewFragment();
            Intent i = this.getIntent();
            wvf.init(i.getExtras());
            getFragmentManager().beginTransaction().add(R.id.webview, wvf).commit();
        }
    }
}

and WebviewFragment:

public class WebViewFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.active_item_detail, container, false);
        WebView wv = (WebView) v.findViewById(R.id.webview);
        wv.getSettings().setJavaScriptEnabled(true);

        //data is the content 
        if(data!=null) {
            wv.loadData(data, "text/html", "UTF-8");
        }
        return v;
    }
}

The layout file is like:

<LinearLayout     xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical">

    <WebView android:id="@+id/webview"
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        tools:context="com.mycompany.myapp.WebViewFragment" />
</LinearLayout>
Chintan Soni
  • 24,761
  • 25
  • 106
  • 174
J.E.Y
  • 1,173
  • 2
  • 15
  • 37

4 Answers4

0
private void emulateShiftHeld(){
 try
  {
    KeyEvent shiftPressEvent = new KeyEvent(0, 0,      
    KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_SHIFT_LEFT, 0, 0);
    System.out.println(“shift press:::”+shiftPressEvent.dispatch(view));

  }
  catch (Exception e){
Log.e(“dd”, “Exception in emulateShiftHeld()”, e);
}

refer to this link https://harshdevchandel.wordpress.com/2013/05/13/android-webview-selection/

check if back is pressed finish this activity

How to handle back button in activity

Community
  • 1
  • 1
0

If a part of your application is contained in a WebView, it may be appropriate for Back to traverse browser history.

@Override
public void onBackPressed() {
    if (mWebView.canGoBack()) {
        mWebView.goBack();
        return;
    }

    // Otherwise defer to system default behavior.
    super.onBackPressed();
}

It is documented in the official docs here
http://developer.android.com/training/implementing-navigation/temporal.html#back-webviews

Aniruddha K.M
  • 7,361
  • 3
  • 43
  • 52
0

Try this way:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                if (wv.canGoBack()) {
                    wv.goBack();
                } else {
                    finish();
                }
                return true;
        }

    }
    return super.onKeyDown(keyCode, event);
}

Also, you try with:

@Override
    public void onBackPressed() {
        Fragment webviewFragment = getSupportFragmentManager().findFragmentByTag("webview");
        if (webview instanceof WebViewFragment) {
            boolean goback = ((WebViewFragment)webview).canGoBack();
            if (!goback)
                super.onBackPressed();
        }
    }
Chintan Soni
  • 24,761
  • 25
  • 106
  • 174
0

Thanks to all. Unfortunately I couldn't make it after trying all suggestions above. I resolved the problem by creating an intent inside the onBackPressed() hook, and start the initial activity.

J.E.Y
  • 1,173
  • 2
  • 15
  • 37