everbody, pls i m working on android application with the side menu navigation drawer with fragment and webview but android back button key is not working. this my code

Main Activity

public class MainActivity extends Activity {String[] menutitles;
TypedArray menuIcons;
String[] pageUrl;

// nav drawer title
private CharSequence mDrawerTitle;
private CharSequence mTitle;

private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;

private List<RowItem> rowItems;
private CustomAdapter adapter;

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mTitle = mDrawerTitle = getTitle();

    menutitles = getResources().getStringArray(R.array.titles);
    menuIcons = getResources().obtainTypedArray(R.array.icons);
    pageUrl = getResources().getStringArray(R.array.pageurl);

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.slider_list);

    rowItems = new ArrayList<RowItem>();

    for (int i = 0; i < menutitles.length; i++) {
        RowItem items = new RowItem(menutitles[i], menuIcons.getResourceId(
                i, -1), pageUrl[i]);
        rowItems.add(items);
    }

    menuIcons.recycle();

    adapter = new CustomAdapter(getApplicationContext(), rowItems);

    mDrawerList.setAdapter(adapter);

    mDrawerList.setOnItemClickListener(new SlideitemListener());

    // enabling action bar app icon and behaving it as toggle button
    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
            R.drawable.ic_drawer, R.string.app_name,
            R.string.app_name) {
        public void onDrawerClosed(View view) {
            getActionBar().setTitle(mTitle);
            // calling onPrepareOptionsMenu() to show action bar icons
            invalidateOptionsMenu();
        }

        public void onDrawerOpened(View drawerView) {
            getActionBar().setTitle(mDrawerTitle);
            // calling onPrepareOptionsMenu() to hide action bar icons
            invalidateOptionsMenu();
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);

    if (savedInstanceState == null) {
        // on first time display view for first nav item
        updateDisplay(0);

    }
}

class SlideitemListener implements ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        updateDisplay(position);
    }

}

private void updateDisplay(int position) {

    String url = rowItems.get(position).getPageUrl();

    Fragment fragment = new MyWebViewFragment();

    Bundle bundle = new Bundle();
    bundle.putString("url", url);

    fragment.setArguments(bundle);

    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction()
            .replace(R.id.frame_container, fragment).commit();

    setTitle(menutitles[position]);
    mDrawerLayout.closeDrawer(mDrawerList);

}

@Override
public void setTitle(CharSequence title) {
    mTitle = title;
    getActionBar().setTitle(mTitle);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // toggle nav drawer on selecting action bar app icon/title
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    // Handle action bar actions click
    switch (item.getItemId()) {
    case R.id.action_settings:
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

/***
 * Called when invalidateOptionsMenu() is triggered
 */
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    // if nav drawer is opened, hide the action items
    boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
    menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
    return super.onPrepareOptionsMenu(menu);
}

/**
 * When using the ActionBarDrawerToggle, you must call it during
 * onPostCreate() and onConfigurationChanged()...
 */

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    // Sync the toggle state after onRestoreInstanceState has occurred.
    mDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    // Pass any configuration change to the drawer toggls
    mDrawerToggle.onConfigurationChanged(newConfig);
}



@Override
 public void onBackPressed() {
       Fragment WebView = getFragmentManager().findFragmentByTag("WebView");
       if (WebView instanceof MyWebViewFragment) {
              boolean goback = ((MyWebViewFragment)WebView).canGoBack();
              if (!goback)
                super.onBackPressed();
       }
 }
}

and my fragment code

public class MyWebViewFragment extends Fragment {

ProgressDialog mProgress;
WebView webview;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.web_fragment, container,
            false);

    Bundle bundle = getArguments();

    String url = bundle.getString("url");

    webview = (WebView) rootView.findViewById(R.id.webview1);

    WebSettings settings = webview.getSettings();
    settings.setJavaScriptEnabled(true);

    mProgress = ProgressDialog.show(getActivity(), "Loading",
            "Please wait for a moment...");
    webview.loadUrl(url);

    webview.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            view.loadUrl(url);
            return true;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            if (mProgress.isShowing()) {
                mProgress.dismiss();
            }
        }
    });

    return rootView;
}

public boolean canGoBack() {
    // TODO Auto-generated method stub
    return false;
}
}

when i press back button on device is not going back, pls help me on it. thanks

use this code

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && browser.canGoBack()) {// check for browser web page history
            browser.goBack(); // back history item
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.