1

我有一个Activity[Activity1],它启动了一个Runnable名为 [ProRunnable] 的对象,并且在其中run()ProRunnable有一个名为 [TExecutor] 的类的实例,我调用了一个方法。

然后该方法启动另一个Activity[Activity2] 以获得结果。以Activity2开头。Callable_ExecutorService

TExecutor包含对 的引用Activity1我怎样才能强制Activity2从外面完成Activity2?我需要Activity2完成并Callable停止运行。

我试过activity1.finishActivity(1)“1”是我用来开始的请求Activity2代码TExecutor。但它从不调用onDestroyof 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();
        }
    }
}

<manifest...>
    <application android:process="package.path.p1">
        <activity android:name="package.path.Activity1"/>
        <activity android:name="package.path.Activity2" android:process="package.path.p2" android:theme="@android:style/Theme.NoDisplay"/>
    </application>
</manifest>
4

2 回答 2

0

尝试after calls的 shutdown Life Cycle中的其他功能。我怀疑这实际上是在接收or ,而不是,它仅由系统根据资源可用性突发奇想调用。Activity2TExecutoractivity1.finishActivity(1)Activity2onPause()onStop()onDestroy()

于 2013-03-22T19:25:17.057 回答
0

我们实际上将它们完全移动到单独的应用程序中。并且改变了相当多的结构。

于 2013-04-03T16:42:58.510 回答