当我按导航抽屉中的“注销”按钮时,该应用程序注销,但进入主页并冻结。我该如何阻止呢?
HomeActivity
public class HomeActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
@BindView(R.id.toolbar)
Toolbar mToolbar;
@BindView(R.id.drawer_layout)
DrawerLayout mDrawer;
@BindView(R.id.nav_view)
NavigationView navigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
ButterKnife.bind(this);
mToolbar.setNavigationIcon(R.drawable.nav_drwaer_option);
changeToolbarTitle("Home");
setSupportActionBar(mToolbar);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, mDrawer, mToolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
toggle.setDrawerIndicatorEnabled(false);
toggle.setToolbarNavigationClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mDrawer.isDrawerOpen(GravityCompat.START)) {
mDrawer.closeDrawer(GravityCompat.START);
} else {
mDrawer.openDrawer(GravityCompat.START);
}
}
});
mDrawer.addDrawerListener(toggle);
toggle.syncState();
navigationView.setNavigationItemSelectedListener(this);
navigationView.setItemIconTintList(null);
DashBoardFragment dashBoardFragment = new DashBoardFragment();
showFragment(dashBoardFragment);
}
public void changeToolbarTitle(String title) {
mToolbar.setTitle(title);
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
if(id == R.id.add_asset){
}
else if(id == R.id.location_update){
gotoSpecificFragment(new AssetListFragment(),"Assets");
}
else if(id == R.id.work_order)
{
}
else if(id == R.id.by_date)
{
}
else if(id == R.id.by_name) {
}
else if(id == R.id.by_work)
{
}
else if(id == R.id.security_setting)
{
}
//problem is here i think
else if (id == R.id.logOutBusiness) {
SaveUserInformationUtil.clearUserInfo(this);
SaveUserInformationUtil.clearUserProfile(this);
// Intent intent = new Intent(HomeActivity.this,HomePage_Register_Login.class);
// startActivity(intent);
finish();
}
// else if(id == R.id.nav_login){
// Intent loginIntent = new Intent(HomeActivity.this,NewLoginActivity.class);
// startActivity(loginIntent);
// }
// else if(id == R.id.nav_register){
// Intent registrationIntent = new Intent(HomeActivity.this, NewRegisterActivity.class);
// startActivity(registrationIntent);
// }
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if(drawer.isDrawerOpen(GravityCompat.START))
drawer.closeDrawer(GravityCompat.START);
else
drawer.openDrawer(GravityCompat.START);
return true;
}
private void showFragment(Fragment fragment) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.dashboard_container, fragment, "Home")
.commit();
}
private void gotoSpecificFragment(Fragment fragment, String title) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.dashboard_container, fragment);
fragmentTransaction.addToBackStack(String.valueOf(mToolbar.getTitle()));
fragmentTransaction.commit();
changeToolbarTitle(title);
}
}
在发生注销之前,我将数据与SharedPreference
一起保存在pref.apply()
中,以进行后台写入和删除操作,并且在注销后,该应用程序返回首页并冻结。 LogCat不显示任何错误。要获取更多参考,请在下面发布logcat信息。
10-09 15:11:31.655 1861-1935/system_process D/ActivityManager: TopActivityInfo, pkgName: com.yumatechnology.yumatelematics activityName: com.yumatechnology.yumatelematics/.activity.HomePage_Register_Login callingPackage: bstSpecialAppKeyboardHandlingEnabled = false
10-09 15:11:31.655 1861-1935/system_process D/ActivityManager: Sending app_launch intent for appName: yumatelematics pkgName: com.yumatechnology.yumatelematics
10-09 15:11:31.655 1861-1935/system_process D/WindowManager: in computeScreenConfigurationLocked() -- hardKeyboardAvailable :true mHardKeyboardAvailable :true mHardKeyboardEnabled :true
10-09 15:11:31.675 1861-3690/system_process D/dalvikvm: GC_FOR_ALLOC freed 2194K, 63% free 9395K/24832K, paused 11ms, total 12ms
10-09 15:11:31.695 1861-3690/system_process D/dalvikvm: GC_FOR_ALLOC freed 2266K, 63% free 9352K/24832K, paused 11ms, total 11ms
10-09 15:11:31.695 1861-3579/system_process V/WindowManager: isVisibleLw false for win : Window{4eb07fb4 u0 SurfaceView}
10-09 15:11:31.705 9715-9744/? E/PGA: eglGetConfigAttrib: Not Implemented: attribute EGL_ALPHA_MASK_SIZE
10-09 15:11:31.705 9715-9744/? E/PGA: eglGetConfigAttrib: Not Implemented: attribute EGL_ALPHA_MASK_SIZE
10-09 15:11:31.705 9715-9744/? E/PGA: eglGetConfigAttrib: Not Implemented: attribute EGL_ALPHA_MASK_SIZE
10-09 15:11:31.705 9715-9744/? E/PGA: eglGetConfigAttrib: Not Implemented: attribute EGL_ALPHA_MASK_SIZE
10-09 15:11:31.705 9715-9744/? E/PGA: eglGetConfigAttrib: Not Implemented: attribute EGL_ALPHA_MASK_SIZE
10-09 15:11:31.705 9715-9744/? E/PGA: eglGetConfigAttrib: Not Implemented: attribute EGL_ALPHA_MASK_SIZE
10-09 15:11:31.705 9715-9744/? E/PGA: eglGetConfigAttrib: Not Implemented: attribute EGL_ALPHA_MASK_SIZE
10-09 15:11:31.705 9715-9744/? E/PGA: eglGetConfigAttrib: Not Implemented: attribute EGL_ALPHA_MASK_SIZE
10-09 15:11:31.705 9715-9744/? E/PGA: eglGetConfigAttrib: Not Implemented: attribute EGL_ALPHA_MASK_SIZE
10-09 15:11:31.705 9715-9744/? E/PGA: eglGetConfigAttrib: Not Implemented: attribute EGL_ALPHA_MASK_SIZE
10-09 15:11:31.705 9715-9744/? E/PGA: eglGetConfigAttrib: Not Implemented: attribute EGL_ALPHA_MASK_SIZE
10-09 15:11:31.705 9715-9744/? E/PGA: eglGetConfigAttrib: Not Implemented: attribute EGL_ALPHA_MASK_SIZE
10-09 15:11:31.705 9715-9744/? E/PGA: eglGetConfigAttrib: Not Implemented: attribute EGL_ALPHA_MASK_SIZE
10-09 15:11:31.705 9715-9744/? E/PGA: eglGetConfigAttrib: Not Implemented: attribute EGL_ALPHA_MASK_SIZE
10-09 15:11:31.705 9715-9744/? E/PGA: eglGetConfigAttrib: Not Implemented: attribute EGL_ALPHA_MASK_SIZE
10-09 15:11:31.705 9715-9744/? E/PGA: eglGetConfigAttrib: Not Implemented: attribute EGL_ALPHA_MASK_SIZE
10-09 15:11:31.725 9715-9744/? W/mbgl: {Thread-484}[OpenGL]: Not using Vertex Array Objects
10-09 15:11:32.455 1861-2288/system_process V/WindowManager: isVisibleLw false for win : Window{4eb07fb4 u0 SurfaceView}
10-09 15:11:32.475 1861-3689/system_process V/WindowManager: isVisibleLw false for win : Window{4eb07fb4 u0 SurfaceView}
10-09 15:11:32.475 1861-2050/system_process D/InputMethodManagerService: packageName=com.yumatechnology.yumatelematics, activityName=.activity.HomePage_Register_Login
10-09 15:11:32.475 1861-2050/system_process D/InputMethodManagerService: ime_enabled = false is same as last value, no change
10-09 15:11:32.475 1861-3663/system_process V/WindowManager: isVisibleLw false for win : Window{4eb07fb4 u0 SurfaceView}
10-09 15:11:32.485 1861-1874/system_process V/WindowManager: isVisibleLw false for win : Window{4eb07fb4 u0 SurfaceView}
10-09 15:11:32.485 1861-1874/system_process V/WindowManager: isVisibleLw false for win : Window{4eb327f0 u0 com.yumatechnology.yumatelematics/com.yumatechnology.yumatelematics.activity.HomeActivity}
10-09 15:11:32.485 1861-1874/system_process V/WindowManager: isVisibleLw false for win : Window{4ee863d0 u0 SurfaceView}
10-09 15:11:32.515 1861-3687/system_process D/dalvikvm: GC_FOR_ALLOC freed 2208K, 63% free 9362K/24832K, paused 18ms, total 18ms
10-09 15:11:32.525 1861-3687/system_process V/WindowManager: isVisibleLw false for win : Window{4eb327f0 u0 com.yumatechnology.yumatelematics/com.yumatechnology.yumatelematics.activity.HomeActivity}
10-09 15:11:32.825 9391-9439/? D/dalvikvm: GC_FOR_ALLOC freed 1794K, 25% free 6239K/8264K, paused 7ms, total 7ms
10-09 15:11:32.885 1514-1514/? W/SurfaceFlinger: couldn't log to binary event log: overflow.
还包括HomeActivity的xml文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_launcher"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@color/textColorWhite"
android:fitsSystemWindows="true"
android:minHeight="?android:listPreferredItemHeightSmall"
app:headerLayout="@layout/nav_header"
app:itemTextColor="@color/textColorBlack"
app:menu="@menu/menu_drawer"
app:theme="@style/NavigationViewTextAppearance" />
</android.support.v4.widget.DrawerLayout>
还要附加menu_drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/add_asset"
android:title="Asset Management"
android:icon="@drawable/add_assets"
>
</item>
<item
android:id="@+id/work_order"
android:title="Work Order"
android:icon="@drawable/list"
/>
<item
android:id="@+id/location_update"
android:title="Location Update"
android:icon="@drawable/list"
/>
<item android:title="Locate Asset"
>
<menu>
<item
android:id="@+id/by_date"
android:title="Locate By Date "
android:icon="@drawable/bangladesh"
>
</item>
<item
android:id="@+id/by_work"
android:title="Locate WorkOrder"
android:icon="@drawable/citymap"
>
</item>
<item
android:id="@+id/by_name"
android:title="Locate Date+Asset"
android:icon="@drawable/citymap"
>
</item>
</menu>
</item>
<item android:title="">
<item
android:id="@+id/security_setting"
android:title="Setting"
android:icon="@drawable/shield"
>
</item>
<item
android:id="@+id/logOutBusiness"
android:title="Log Out"
android:icon="@drawable/shield"
>
</item>
</item>
答案 0 :(得分:0)
您可以尝试根据以下答案改编代码:https://stackoverflow.com/a/5140545/3780625
在菜单中尝试:
Intent intent = new Intent(getApplicationContext(), HomePage_Register_Login.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("LOGOUT", true);
startActivity(intent);
然后在您的Login活动中,在onCreate上执行任何其他操作之前,请清除数据。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getIntent().getBooleanExtra("LOGOUT", false)) {
SaveUserInformationUtil.clearUserInfo(this);
SaveUserInformationUtil.clearUserProfile(this);
}
setContentView(R.layout.activity_home);
}
希望有帮助!