问题描述
我使用一个按钮退出我的应用程序,但是,当我退出它并按下最近的应用程序按钮时(下图) 并通过灰屏重新进入我的应用程序,而不是从主活动启动应用程序,它从最近的活动启动并崩溃,因为它有 bo 数据.. 即使应用程序崩溃,当我再次按下灰色活动时,它会再次打开第二个活动并再次崩溃..
这是我的代码:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Stop Navigation ");
builder.setMessage("Are you sure you want to stop navigation?");
builder.setCancelable(false);
builder.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
exitButtonpressed = true;
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
//finishAffinity();
//System.exit(0);
//finishAndRemoveTask();
//android.os.Process.killProcess(android.os.Process.myPid());
// I TRIED ALL THE ABOVE BUT ITS ITS NOT WORKING..
}
});
谢谢!!
编辑:
当点击灰色方块时它会激活 OnResume..
我注意到每次我删除我的任务并通过我的前台服务进入它发生的第二个活动时。 我认为这是关于未决意图。
这是我待处理的意图代码:
Intent i = new Intent(getApplicationContext(),MapsActivity.class);
i.putExtra("AddressBackground22",mAddress);
i.putExtra("Addresstype22",mAddresstype);
i.putExtra("AddressLatBackground22",destinationLat);
PendingIntent activityPendingIntent = PendingIntent.getActivity(this,25,i,0);
NotificationCompat.Action action2 = new NotificationCompat.Action(R.drawable.ic_cancel,getString(R.string.remove_location_updates),servicePendingIntent);
NotificationCompat.Action action = new NotificationCompat.Action(R.drawable.ic_launch,getString(R.string.launch_activity),activityPendingIntent);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this,NOTIFICATION_CHANNEL_ID);
Notification notification = new NotificationCompat.Builder(this,NOTIFICATION_CHANNEL_ID)
.setContentTitle("App is running in background")
.addAction(action)
.addAction(action2)
.setColor(getColor(R.color.foreGroundBackgroundColor))
.setPriority(notificationmanager.IMPORTANCE_HIGH)
.setCategory(Notification.CATEGORY_SERVICE)
.setSmallIcon(R.drawable.applogo)
.build();
解决方法
你为什么使用
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
只需调用
finish();
点击按钮
,YourActivity.this.finish();
您应该在完成活动之前关闭对话框
Dialog.dismiss();
希望可能会有所帮助。