@OnClick(R.id.btn_user_logout)
public void logout(View view){//"退出登录"button的回调方法//1.将保存在sp中的数据清除SharedPreferences sp = this.getSharedPreferences("user_info", Context.MODE_PRIVATE);sp.edit().clear().commit();//清除数据操作必须提交;提交以后,文件仍存在,只是文件中的数据被清除了//2.将本地保存的图片的file删除File filesDir;if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){//判断sd卡是否挂载//路径1:storage/sdcard/Android/data/包名/filesfilesDir = this.getExternalFilesDir("");}else{//手机内部存储//路径:data/data/包名/filesfilesDir = this.getFilesDir();}File file = new File(filesDir,"icon.png");if(file.exists()){file.delete();//删除存储中的文件}//3.销毁所有的activitythis.removeAll();//4.重新进入首页面this.goToActivity(MainActivity.class,null);
}
用户退出:
1.销毁当前activity
@OnClick(R.id.iv_top_back)
//返回按钮的退出操作
@OnClick(R.id.iv_top_back)
public void back(View view) {ActivityManager.getInstance().removeCurrent();
}
2.重写MeFragment的onResume()
/*** 当当前的Fragment显示时,考虑是否需要从本地读取用户头像*/
@Override
public void onResume() {super.onResume();Log.e("TAG", "onResume");readImage();
}private boolean readImage() {File filesDir;//判断sd卡是否处于挂载状态if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){filesDir = this.getActivity().getExternalFilesDir(null);//sdcard/Android/data/应用包名/file/...jpg}else{filesDir = this.getActivity().getFilesDir();}File file = new File(filesDir,"icon.png");if (file.exists()) {//如果存在//存储--->内存Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());ivMeIcon.setImageBitmap(bitmap);return true;}return false;
}