简体   繁体   中英

While pressing back button navigation drawer is opening instead of previous activity

My application contains a Navigation Drawer where each option starts a new Activity.they all have the same Navigation Drawer.

The problem is that whenever i press back button (in any activity), the Navigation Drawer is coming instead of activity

        public class ReportProblem extends Activity {

        WebView mWebView;

     SimpleSideDrawer rightSlider;
    LinearLayout ll_menu, ll_start_stop;
    Context mContext = this;

    private RelativeLayout rl_main;

    private LoaderProgress loader;
    Utillity utillity;

private ImageView iv_browser;

private CustomText tv_open_in_browser;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_report_problem);

    utillity =  new Utillity(mContext);

    ll_menu = (LinearLayout) findViewById(R.id.ll_menu);

    rightSlider = new SimpleSideDrawer(this);

    rightSlider.setLeftBehindContentView(R.layout.menu_drawer);

    rl_main = (RelativeLayout) findViewById(R.id.rl_main);

    menuClick();
    ll_menu.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            rightSlider.toggleLeftDrawer();

        }
    });

    rl_main.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

        }
    });

    loader = new LoaderProgress(mContext);

    mWebView = (WebView) findViewById(R.id.web_activity_summery);
    mWebView.getSettings().setJavaScriptEnabled(true);


    iv_browser=(ImageView)findViewById(R.id.iv_browser);

    tv_open_in_browser=(CustomText)findViewById(R.id.tv_open_in_browser);

    iv_browser.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(v.getTag().toString().equals("0"))
            {
                v.setTag("1");
                tv_open_in_browser.setVisibility(View.VISIBLE);
            }
            else
            {
                v.setTag("0");
                tv_open_in_browser.setVisibility(View.GONE);
            }
        }
    });


    tv_open_in_browser.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            String base64 = Base64.encodeToString(ParseUser.getCurrentUser()
                    .getObjectId().toString().getBytes(), Base64.DEFAULT);

                Intent browserIntent = new Intent(Intent.ACTION_VIEW);
                browserIntent.setData(Uri.parse("http://.com/app/feedback/index.html?id="+URLEncoder.encode(base64)));
                startActivity(browserIntent);
                v.setTag("0");
                tv_open_in_browser.setVisibility(View.GONE);
        }
    });


    mWebView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            if(iv_browser.getTag().toString().equals("1"))
            {
                iv_browser.setTag("0");

                tv_open_in_browser.setVisibility(View.GONE);
            }

        }
    });







    if (utillity.isNetworkAvailable()) {

        String base64 = Base64.encodeToString(ParseUser.getCurrentUser()
                .getObjectId().toString().getBytes(), Base64.DEFAULT);
        mWebView.loadUrl("http://com/app/feedback/index.html?id="+URLEncoder.encode(base64));

    } else {

        showAlertDialog("Please make sure that you have an active Internet connection");
    }

    new GpsSharedPrefrence(mContext)
            .setActivityState(GpsSharedPrefrence.ActivityResume);

}



@Override
protected void onResume() {
    // TODO Auto-generated method stub

    new GpsSharedPrefrence(mContext)
            .setActivityState(GpsSharedPrefrence.ActivityResume);

    super.onResume();
}

@Override
protected void onPause() {
    new GpsSharedPrefrence(mContext)
            .setActivityState(GpsSharedPrefrence.ActivityPause);
    super.onPause();
}

public void menuClick() {

    View menuView = rightSlider.getLeftBehindView();

    LinearLayout ll_activity, ll_history, ll_configure_device, ll_settings, ll_activity_summery, ll_logout;

    ll_activity = (LinearLayout) menuView.findViewById(R.id.ll_activity);
    ll_history = (LinearLayout) menuView.findViewById(R.id.ll_history);
    ll_configure_device = (LinearLayout) menuView
            .findViewById(R.id.ll_configure_device);
    ll_settings = (LinearLayout) menuView.findViewById(R.id.ll_settings);
    ll_activity_summery = (LinearLayout) menuView
            .findViewById(R.id.ll_activity_summery);
    ll_logout = (LinearLayout) menuView.findViewById(R.id.ll_logout);
    LinearLayout ll_report_problem = (LinearLayout) menuView.findViewById(R.id.ll_report_problem);
    ll_report_problem.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            rightSlider.toggleLeftDrawer();

            // Intent intentActivity = new Intent(mContext,
            // Activities.class);
            // startActivity(intentActivity);
            // finish();
        }
    });
    LinearLayout ll_treadmill = (LinearLayout) menuView.findViewById(R.id.ll_treadmill);


    ll_treadmill.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {


            Intent intentActivity = new Intent(mContext,
                    TreadMillActivity.class);
            Bundle animation=ActivityOptions.makeCustomAnimation(getApplicationContext(), 
                    R.anim.animation, R.anim.animation2).toBundle();
            startActivity(intentActivity,animation);


        }
    });

    ll_activity.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intentActivity = new Intent(mContext, Activities.class);
            Bundle animation=ActivityOptions.makeCustomAnimation(getApplicationContext(),
                    R.anim.animation, R.anim.animation2).toBundle();
            startActivity(intentActivity,animation);

        }
    });
    ll_history.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intentActivity = new Intent(mContext,
                    HistoryActivity_web.class);
            Bundle animation=ActivityOptions.makeCustomAnimation(getApplicationContext(), 
                    R.anim.animation, R.anim.animation2).toBundle();
            startActivity(intentActivity,animation);

        }
    });
    ll_configure_device.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intentActivity = new Intent(mContext,
                    InsoleConnection.class);
            Bundle animation=ActivityOptions.makeCustomAnimation(getApplicationContext(),
                    R.anim.animation, R.anim.animation2).toBundle();
            startActivity(intentActivity,animation);

        }
    });
    ll_settings.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intentActivity = new Intent(mContext,
                    SettingsActivity.class);
            Bundle animation=ActivityOptions.makeCustomAnimation(getApplicationContext(),
                    R.anim.animation, R.anim.animation2).toBundle();
            startActivity(intentActivity,animation);

        }
    });

    ll_activity_summery.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intentActivity = new Intent(mContext,
                    ActivitySummery.class);
            Bundle animation=ActivityOptions.makeCustomAnimation(getApplicationContext(),
                    R.anim.animation, R.anim.animation2).toBundle();
            startActivity(intentActivity,animation);

        }
    });

    ll_logout.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            new UserSharedPrefrence(mContext).clearPrefrence();
            new BleSharedPrefrence(mContext).clearPrefrence();
            finish();
        }
    });

}

public void showAlertDialog(String Message) {
    final Dialog dialog = new Dialog(mContext);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.getWindow().setBackgroundDrawable(
            new ColorDrawable(android.graphics.Color.TRANSPARENT));
    dialog.setCancelable(false);
    dialog.setContentView(R.layout.cart_dialog);

    TextView txtdetail = (TextView) dialog.findViewById(R.id.txtdetail);
    TextView txtno = (TextView) dialog.findViewById(R.id.txtno);

    txtno.setVisibility(View.GONE);

    TextView txtyes = (TextView) dialog.findViewById(R.id.txtyes);

    txtyes.setText("Ok");

    txtdetail.setText(Message);
    txtyes.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            dialog.dismiss();
        }
    });
    dialog.show();
}

@Override
public void onBackPressed() {
    if(rightSlider.isDrawerOpen(GravityCompat.START)){
        rightSlider.closeDrawer(GravityCompat.START);
      }
       else{

   super.onBackPressed();

    }

 }
 }

First of all, having a Navigation Drawer on every Activity is a bad practice and a bad design pattern.

You should have one activity with different fragments for every option on your navigation drawer. Android Navigation Drawer

If you decide to keep your current code, I think your problem is you are not closing the navigation drawer before opening a new activity.Then when you go back to the previous Activity your NavigationDrawer is still open. To close it call "closeDrawers" on your DrawerLayout: closeDrawers()

Handle your backpress in your Navigation Drawer activity by using the method onBackPressed() or by looking code.

 public void onBackPressed()
{
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    }
  //And other code you want on backpress.
}

And also in onNavigationItemSelected(MenuItem item) method, after selecting menu you have to close the drawer.

 public boolean onNavigationItemSelected(MenuItem item)
{
//menus here
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM