从webview中左侧的导航恢复中重新打开选项卡时

时间:2018-04-21 10:09:48

标签: android hyperlink tabs android-webview resume

这是我的代码。我希望当我从导航打开webview时,它必须从我离开的地方继续,或者当我更改我的标签时,最后一个必须从我离开标签的地方恢复。请帮帮我!

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {
    private WebView wv;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    wv = (WebView)findViewById(R.id.wv);
    WebSettings webSettings=wv.getSettings();
    webSettings.setJavaScriptEnabled(true);
    wv.loadUrl("http://google.com");
        wv.setWebViewClient(new WebViewClient());
}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.Google) {
        wv.loadUrl("http://google.com");
    } else if (id == R.id.Facebook) {
        wv.loadUrl("http://m.facebook.com");
    } else if (id == R.id.Youtube) {
        wv.loadUrl("http://youtube.com");
    } else if (id == R.id.nav_share) {

    } else if (id == R.id.nav_send) {

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event){
    if ((keyCode==KeyEvent.KEYCODE_BACK)&& wv.canGoBack()){
        wv.canGoBack();
        return true;
    }
        return super.onKeyDown(keyCode, event);
}
}

1 个答案:

答案 0 :(得分:0)

在触摸导航项目时保存网址。然后在修饰动作发生时加载url。