简体   繁体   中英

Finishing a child Activity, in a different process, from parent Activity. finishActivity() not working

I have an Activity [Activity1] that starts a Runnable called [ProRunnable] and in the run() of ProRunnable i have an instance of a class called [TExecutor] that i call a method on.

That method then starts another Activity [Activity2] for result. The Activity2 starts a Callable with ExecutorService .

TExecutor contains a reference to Activity1 . How can i force Activity2 to finish from outside Activity2 ? I need Activity2 to finish and the Callable to stop running too.

I have tried activity1.finishActivity(1) where "1" is the request code i used to start Activity2 in TExecutor . But it never calls the onDestroy of Activity2 . And i do not see that it is trying to finish the Activity2 .

After i make the call for the activity to finish, it does not appear to go into onDestroy and the callable continues to run.

Activity2 has no theme, so it basically runs in the background. It is also in a different process. I set all this in the manifest, shown at bottom.

Sounds confusing, i know. If you need more information, let me know.

Thanks


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>

Try the other functions in the shutdown Life Cycle of Activity2 after TExecutor calls activity1.finishActivity(1) . I suspect that Activity2 is actually receiving onPause() or onStop() , just not onDestroy() , which is only invoked by system whim based on resources availability.

We actually moved them into separate apps completely. And changed the structure quite a bit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM