finsh()杀死进程 第一次无效返回上一次活动 第二次才能杀死

点按Button3,会回到SecondActivity ,再进入thirdActivity 点按button3 才能杀死程序

package com.example.activitytest;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class ThirdActivity extends BaseActivity {

@Override
protected void onDestroy() {
    super.onDestroy();
    Log.d("ThirdActivity","onDestroy");

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d("ThirdActivity","Task id is " + getTaskId());
    setContentView(R.layout.third_layout);
    Button button3 = (Button) findViewById(R.id.button_3);
    button3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ActivityCollector.finishAll();
        }
    });
}

}

package com.example.activitytest;

import android.app.Activity;

import java.util.ArrayList;
import java.util.List;

public class ActivityCollector {

public static List<Activity> activities = new ArrayList<>();

public static void addActivity(Activity activity) {
    activities.add(activity);
}

public static void removeActivity(Activity activity) {
    activities.remove(activity);
}

public static void finishAll() {
    for (Activity activity : activities) {
        if (!activity.isFinishing()) {
            activity.finish();
            android.os.Process.killProcess(android.os.Process.myPid());
        }
    }
    activities.clear();
}

}

点按button 3 返回Secondactivity时候的logcat
12-26 23:31:29.667 4664-4664/? I/art: Late-enabling -Xcheck:jni
12-26 23:31:29.668 4664-4664/? W/art: Unexpected CPU variant for X86 using defaults: x86
12-26 23:31:30.152 4664-4664/com.example.activitytest W/System: ClassLoader referenced unknown path: /data/app/com.example.activitytest-2/lib/x86
12-26 23:31:30.202 4664-4664/com.example.activitytest I/InstantRun: starting instant run server: is main process
12-26 23:31:30.419 4664-4664/com.example.activitytest W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
12-26 23:31:30.460 4664-4664/com.example.activitytest D/BaseActivity: SecondActivity
12-26 23:31:30.474 4664-4664/com.example.activitytest D/SecondActivity: Task id is 115
12-26 23:31:31.403 4664-4681/com.example.activitytest D/libEGL: Emulator has host GPU support, qemu.gles is set to 1.
12-26 23:31:31.406 4664-4681/com.example.activitytest E/libEGL: load_driver(/system/lib/egl/libGLES_emulation.so): dlopen failed: library "/system/lib/egl/libGLES_emulation.so" not found
12-26 23:31:31.413 4664-4681/com.example.activitytest D/libEGL: loaded /system/lib/egl/libEGL_emulation.so
12-26 23:31:31.418 4664-4681/com.example.activitytest D/libEGL: loaded /system/lib/egl/libGLESv1_CM_emulation.so
12-26 23:31:31.452 4664-4681/com.example.activitytest D/libEGL: loaded /system/lib/egl/libGLESv2_emulation.so
12-26 23:31:31.534 4664-4681/com.example.activitytest I/OpenGLRenderer: Initialized EGL, version 1.4
12-26 23:31:31.534 4664-4681/com.example.activitytest D/OpenGLRenderer: Swap behavior 1
12-26 23:31:31.584 4664-4664/com.example.activitytest W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
12-26 23:31:31.713 4664-4681/com.example.activitytest E/EGL_emulation: tid 4681: eglSurfaceAttrib(1174): error 0x3009 (EGL_BAD_MATCH)
12-26 23:31:31.713 4664-4681/com.example.activitytest W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xd73bace0, error=EGL_BAD_MATCH

5个回答

public static void finishAll() {
for (Activity activity : activities) {
if (!activity.isFinishing()) {
activity.finish();
android.os.Process.killProcess(android.os.Process.myPid());
}
}
activities.clear();
}
试试改成:
public static void finishAll() {
for (Activity activity : activities) {
if (!activity.isFinishing()) {
activity.finish();
}
}
activities.clear();
android.os.Process.killProcess(android.os.Process.myPid());
}

u013835855
山洼之神 回复weixin_38335734: 我只能说你原来的程序写法,一看就有很多不可控的问题,关于这个问题产生的具体细节其实也不是很好跟踪,.killProcess在执行时具体做了那些事要进堆栈里面才能具体分析。你说的只是慢一点的问题并不能解释为什么thirdactivity被finish掉了,同时,第二次的时候为什么又能正确执行。你需要查看详细堆栈信息才能真正明白原因。
大约 2 年之前 回复
weixin_38335734
-Tiger- 谢谢 是这个问题 finish慢一点 killprocess更快 所以finish还没完成 就先把活动杀掉了
大约 2 年之前 回复

静态方法回收资源本身就慢,再加上finish之后有时候还不执行onDestroy方法,对实时行要求高时,全部手动回收资源。

weixin_38335734
-Tiger- 谢谢了,是这个问题。不过我只能把分给楼上了,楼上先回答的。谢谢
大约 2 年之前 回复

是不是第一执行执行时给指定返回不正确啊?

经验告诉你不要这样写,这样很可能前面的Activity因为回收已经被销毁了,重启后又可以用,要么singleTask,要么:

 Intent intent = new Intent(AActivity.this, LoginActivity.class);
 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

这样的方式比较可靠,不要太相信书本,当初好像也是从书上看到类似的方法,但实际开发并不合适这样做Activity管理。

像这种问题最好发个gif图来看才能懂它的意思。。。。。。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问