完成子活动,在不同的过程中,从父活动。 finishActivity()不起作用

时间:2013-03-22 18:45:59

标签: java android

我有Activity [Activity1]启动Runnable调用[ProRunnable],在run() ProRunnable我有一个名为[TExecutor的类的实例]我称之为方法。

然后该方法为结果启动另一个Activity [Activity2]。 Activity2Callable开始ExecutorService

TExecutor包含对Activity1的引用。 如何强制Activity2从外部Activity2结束?我需要Activity2完成,Callable也要停止运行。

我尝试了activity1.finishActivity(1),其中“1”是用于在Activity2中启动TExecutor的请求代码。但它永远不会调用onDestroy的{​​{1}}。而且我没有看到它正在尝试完成Activity2

在我完成活动的调用之后,它似乎没有进入Activity2并且可调用的内容继续运行。

onDestroy没有主题,因此它基本上在后台运行。它也处于不同的过程中。我在清单中设置了所有这些,显示在底部。

听起来令人困惑,我知道。如果您需要更多信息,请与我们联系。

由于


Activity2

public class Activity1 extends FragmentActivity {

    private ProRunnable proRunnable;
    private Thread thread;

    public onStart() {

        this.proRunnable = new ProRunnable(this);
        this.thread = new Thread(this.proRunnable);
        this.thread.start();
    }

    ON BUTTON PRESS {

        this.finishActivity(Activity2.REQUEST_CODE);
    }

    protected void onActivityResult(int reqCode, int resCode, Intent data) {

        if(reqCode == Activity2.REQUEST_CODE) {

            //do stuff
        }
    }
}

public class ProRunnable implements Runnable {

    private TExecutor tExecutor;
    private Activity activty1;

    public ProRunnable(Activity activity) {

        this.activity1 = activity;
    }

    public void run() {

        this.tExecutor = new TExecutor(this.activity1);
        this.tExecutor.execute();
    }
}

public final class TExecutor {

    private final Activity activity1;
    private Intent activity2Intent;

    public TExecutor(Activity activity) {

        this.activity1 = activity;
    }

    public void execute() {

        this.activity2Intent = new Intent(activity1.getBaseContext(), Activity2.class);
        this.activity2Intent.startActivityForResult(this.activity2Intent, Activity2.REQUEST_CODE);
    }
}

public class Activity2 extends Activity {

    public static final int REQUEST_CODE = 1;
    private ExecutorService executor;

    protected void onStart() {

        Intent returnIntent = new Intent();
        InsertThread insert = new InserterThread();
        this.executor = Executors.newSingleThreadExecutor();
        Future<Boolean> submit = this.executor.submit(inserter);

        try {

            submit.get();
        } catch ... {
        }
    }

    private class InserterThread implements Callable<Boolean> {

        public InserterThread() {

        }

        public Boolean call() throws ... {

            while(!Thread.currentThread().isInterrupted()) {

                for(...) {

                    if(Thread.currentThread().isInterrupted()) {

                        break;
                    }
                }
            }
        }

        protected void onDestroy() {

            super.onDestroy();
            Log.e(TAG, "DESTROYING THE ACTIVITY");
            this.executor.shutdownNow();
        }
    }
}

2 个答案:

答案 0 :(得分:0)

Activity2来电TExecutor之后,尝试activity1.finishActivity(1)的关闭Life Cycle中的其他功能。我怀疑Activity2实际上正在接收onPause()onStop(),而不是onDestroy(),这只是由系统突发奇想根据资源可用性调用。

答案 1 :(得分:0)

我们实际上已将它们完全转移到单独的应用中。并且改变了结构。