【发布时间】:2021-02-23 12:14:22
【问题描述】:
我使用一个按钮退出我的应用程序,但是,当我退出它并按下最近的应用程序按钮时(下图) 并通过灰屏重新进入我的应用程序,而不是从主要活动启动应用程序,它从最近的活动开始并崩溃,因为它有 bo 数据.. 即使应用程序崩溃了,当我再次按下灰色活动时,它再次打开第二个活动并再次崩溃..
灰屏图片(https://ibb.co/TqPnXJR)
如何解决当用户点击灰屏时会启动主要活动的问题?
这是我的代码:
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();
【问题讨论】:
标签: android android-intent exit activity-finish