weixin_38335734
-Tiger-
采纳率33.3%
2017-12-27 04:39

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

5
已采纳

点按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条回答

  • u013835855 山洼之神 4年前

    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());
    }

    点赞 1 评论 复制链接分享
  • morgerton 白萝卜blb 4年前

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

    点赞 1 评论 复制链接分享
  • YXTS122 YXTS122 4年前

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

    点赞 评论 复制链接分享
  • yu_duan_hun yu_duan_hun 4年前

    经验告诉你不要这样写,这样很可能前面的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管理。

    点赞 评论 复制链接分享
  • zhanghl8207 海丽 4年前

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

    点赞 评论 复制链接分享