我正在制作一个应用程序,我想确保只有一个活动的一个实例应该保留在堆栈中。请告诉我如何实现这一点。这是我正在尝试但没有得到预期结果的编码。
ArrayList<String> runningactivities = new ArrayList<String>();
ActivityManager activityManager = (ActivityManager)getBaseContext().getSystemService (Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> services = activityManager.getRunningTasks(Integer.MAX_VALUE);
for (int i1 = 0; i1 < services.size(); i1++) {
runningactivities.add(0,services.get(i1).topActivity.toString());
}
if(runningactivities.contains("ComponentInfo{com.velosys.interview_preparation/com.velosys.interview_preparation.activities.MCQ}")==true){
Toast.makeText(getBaseContext(),"Activity is in foreground, active",1000).show();
//MCQ.
}
提前致谢