问题:
多activity中退出整个程序,例如从A->B->C->D>E,这时我需要从E直接退出程序。
finish()和system(0)都只能退出单个activity。杀进程等的等方式都不行~~~
解决问题:
我们知道Android的窗口类提供了历史栈,我们可以通过stack的原理来巧妙的实现,这里我们在D窗口打开A窗口时在Intent中直接加入标志Intent.FLAG_ACTIVITY_CLEAR_TOP,再次开启A时将会清除该进程空间的所有Activity。
在D中使用下面的代码:
- Intent intent = new Intent();
- intent.setClass(D.this, A.class);
- intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); //注意本行的FLAG设置
- startActivity(intent);
- finish();//关掉自己
在A中加入代码:
- //Override
- protected void onNewIntent(Intent intent) {
- // TODO Auto-generated method stub
- super.onNewIntent(intent);
- //退出
- if ((Intent.FLAG_ACTIVITY_CLEAR_TOP & intent.getFlags()) != 0) {
- finish();
- }
- }
A的Manifest.xml配置成android:launchMode="singleTop"
原理总结:
一般A是程序的入口点,从D起一个A的activity,加入标识Intent.FLAG_ACTIVITY_CLEAR_TOP这个过程中会把栈中B,C,都清理掉。因为A是android:launchMode="singleTop"
不会调用oncreate(),而是响应onNewIntent()这时候判断Intent.FLAG_ACTIVITY_CLEAR_TOP,然后把A finish()掉。
栈中A,B,C,D全部被清理。所以整个程序退出了。
个人补充:
1.可以把A设置成不可见的Acitivity(方法见下面),然后在它的onCreate方法里跳转到“真正”的载入界面
就可以实现在D中点退出程序按钮时看上去立即退出程序的效果
2.A必须是程序启动的第一个Activity才能起到这种立即退出的效果,因为Intent.FLAG_ACTIVITY_CLEAR_TOP只会把目标Activity的“上面”的Activity清理掉,而如果目标Activity的“下面”还有Activity(换句话说,目标Activity不在栈底),则finish后只会到他下面的那个Activity,而不是立即退出的效果了
3.不可见Activity
在项目的AndroidManifest.xml文件中相应的Activity标签中添加这样一行:
android:theme=”@android:style/Theme.NoDisplay”
这样一来,当这个Activity启动的时候,就不会显示出界面了。
相关推荐
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); finishAffinity(); } ``` 2. **使用FLAG_ACTIVITY_NEW_TASK和FLAG_ACTIVITY_CLEAR_TASK**:在...
shortcutIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); // 快捷图标 ShortcutIconResource iconRes = Intent....
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); return intent; } ``` **解析**: - `setDataAndType(uri, "application/pdf")`:设置数据和MIME类型。 - `setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY)`:设置...
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); ``` 1. **创建Intent对象**: - `Intent intent = new Intent(BaseImageMenuActivity.this, MainMenuActivity.class);` 这行代码创建了一个显式的Intent对象,...
如果用户希望直接返回到`Activity A`,而不想看到中间的过程,这时就可以使用`Intent.FLAG_ACTIVITY_CLEAR_TOP`标志来实现快速返回功能。 示例代码如下: ```java // 在D中返回到A Intent intent = new Intent(); ...
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); startActivity(intent); ``` 这段代码会启动主屏幕(ACTION_MAIN),同时设置`FLAG_ACTIVITY_NEW_TASK`和`FLAG_ACTIVITY_...
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.putExtra("exit", true); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); ...
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); startActivity(intent); ``` 通过这种方式,我们可以使用`Activity`来创建弹出式对话框,提供更多的自定义和交互性。但是要...
2. **使用FLAG_ACTIVITY_NEW_TASK和FLAG_ACTIVITY_CLEAR_TOP**:在启动 Launcher 的 Intent 上添加这两个标志,这样可以确保当 Launcher 已经在栈顶时,再次启动不会创建新的实例,而是回到现有的实例。 ```java ...
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); startActivity(intent); } } ``` 3. **实现`finishAllActivities`方法**:这个方法用于遍历并结束当前任务栈中的所有...
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); startActivity(intent); System.exit(0); // 退出进程 } } ``` 在`exitApp()`方法中,首先获取当前正在运行的任务,然后...
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); startActivity(intent); ``` 三、注意事项 - 重启设备可能会导致未保存的数据丢失,因此在进行此操作时需谨慎。 - 对于...
smallWindowIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); context.startActivity(smallWindowIntent); } } } // 在AndroidManifest.xml中注册广播接收器 ...
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); overridePendingTransition(R.anim.activity_in, R.anim.activity_out); startActivity(intent); ``` 其次,防止背景黑屏问题...
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); startActivity(intent); break; } } } // 如果有后台服务,需要在这里停止 // ... // 清理其他资源 // ... // ...
shortcutIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); ``` 这里设置了`FLAG_ACTIVITY_NEW_TASK`和`FLAG_ACTIVITY_CLEAR_TASK`标志,确保只启动目标Activity,不带任何之前的...
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); ``` 或者在`styles.xml`中定义: ```xml <item name="android:activityOpenEnterAnimation">@anim/slide_in_right ...
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); android.os.Process.killProcess(android.os.Process.myPid()); System.exit(0); } ``` 这个方法...
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left); ``` 这里的`R.anim.slide_in_right`和`R.anim.slide_out_...
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); intent.putExtra("animation", R.anim.enter); // 传递进入动画资源ID startActivity(intent); overridePendingTransition(R....