我的应用程序包含一个导航抽屉,每个选项都会启动一个新的Activity.they都有相同的导航抽屉。
问题在于,无论何时按下后退按钮(在任何活动中),导航抽屉都会来,而不是活动
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();
}
}
}
答案 0 :(得分:1)
首先,在每个Activity上都有一个导航抽屉是一种不好的做法和糟糕的设计模式。
导航抽屉上的每个选项都应该有一个包含不同片段的活动。 Android Navigation Drawer
如果您决定保留当前代码,我认为您的问题是在打开新活动之前没有关闭导航抽屉。然后当您返回上一个活动时,您的NavigationDrawer仍处于打开状态。 要关闭它,请打电话给#34; closeDrawers"在你的DrawerLayout上: closeDrawers()
答案 1 :(得分:0)
使用方法 onBackPressed()或查看代码,在导航抽屉活动中处理背景。
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.
}
并且在 onNavigationItemSelected(MenuItem item)方法中,选择菜单后,您必须关闭抽屉。
public boolean onNavigationItemSelected(MenuItem item)
{
//menus here
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}