简体   繁体   English

按下后退按钮时,导航抽屉将打开,而不是先前的活动

[英]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. 我的应用程序包含一个导航抽屉,其中每个选项都启动一个新的Activity。它们都具有相同的导航抽屉。

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. 首先,在每个Activity上都有一个导航抽屉是一种不好的做法,也是一种不好的设计模式。

You should have one activity with different fragments for every option on your navigation drawer. 您应该对导航抽屉上的每个选项进行一项活动,并使用不同的片段。 Android Navigation Drawer Android导航抽屉

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. 如果您决定保留当前代码,我想您的问题是您没有在打开新活动之前关闭导航抽屉。然后,当您返回上一个活动时,NavigationDrawer仍然处于打开状态。 To close it call "closeDrawers" on your DrawerLayout: closeDrawers() 要关闭它,请在您的DrawerLayout上调用“ closeDrawers”: closeDrawers()

Handle your backpress in your Navigation Drawer activity by using the method onBackPressed() or by looking code. 通过使用onBackPressed()方法或查看代码来处理Navigation Drawer活动中的backpress

 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. 同样在onNavigationItemSelected(MenuItem item)方法中,选择菜单后,您必须关闭抽屉。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 按下后退按钮时,应用程序将关闭,同时在单个活动中使用导航抽屉中的片段 - On pressing back button app closes while using fragments in navigation drawer in single activity 使用导航组件时按下后退按钮退出应用程序而不是导航到上一个屏幕 - Pressing back button exits the app instead of navigating to the previous screen while using navigation component 如何让向上按钮返回而不是打开导航抽屉 - How to make Up Button go back instead of opening Navigation Drawer 当按下导航栏后退按钮时,Android活动将返回启动它而不是父活动的活动 - Android activity go back to activity that started it instead of parent activity when pressing navigation bar back button 单击ActionBar后退导航按钮时,活动未返回到先前的活动 - Activity is not returning to previous activity while clicking on ActionBar back navigation button 按下返回按钮不会显示上一个活动 - Previous Activity does not come on pressing back button 通过按返回按钮重新启动上一个活动 - Restarting the previous activity by pressing back button 从一个活动按下后退按钮可刷新堆栈中的上一个活动 - Pressing back button from one activity refreshes previous activity in stack go 如何通过按下带有一些数据的后退按钮返回上一个活动? - How to go back to the previous activity by pressing the back button with some data? 按后退按钮没有回到以前的Android活动 - Pressing Back Button did not go back to previous activity Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM