简体   繁体   English

来自异步子类的Android应用程序finish()

[英]Android application finish() from Async Subclass

I am using an Async Class as a subclass. 我正在使用异步类作为子类。 After the success successful completion, I want to go to an activity, suppose ListActivity. 成功成功完成之后,我想参加一个活动,假设为ListActivity。

But after reach ListActivity, When I click on "back" it coming back to the previous activity itself. 但是到达ListActivity后,当我单击“返回”时,它将返回到上一个活动本身。 So I tried "finish()". 所以我尝试了“ finish()”。 But I am getting an error. 但是我遇到了错误。

My code looks like. 我的代码看起来像。

protected void onPostExecute(String result) {

        if(flags.equals(1)){
            Intent homepage = new Intent(GCMIntentService.this, ListActivity.class);
            homepage.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(homepage);
            finish();
        }
        else {

Can any one have any idea about how to use finish() in Async Subclass ? 任何人都可以对如何在异步子类中使用finish()有所了解吗?

Thanks in advance 提前致谢

Delete your current activity after start the intent, calling currentActivity.this.finish(); 启动意图后,删除当前活动,调用currentActivity.this.finish();

    protected void onPostExecute(String result) {

        if(flags.equals(1)){
            Intent homepage = new Intent(GCMIntentService.this, ListActivity.class);
            homepage.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(homepage);
            currentActivity.this.finish()
            ...
         }

YourClassName.this.finish() where YourClassName is the name of the class that contains the AsyncTask . YourClassName.this.finish()其中YourClassName是包含AsyncTask的类的名称。

EDIT: GCMIntentService is actually an instance of GCMBaseIntentService , not an Activity . 编辑: GCMIntentService实际上是GCMBaseIntentService一个实例,而不是Activity Therefore it has no finish() method. 因此,它没有finish()方法。 The correct way to stop a service is by calling stopSelf() (doc here ). 停止服务的正确方法是调用stopSelf() (doc 在此处 )。 So, to stop your service you should call GCMIntentService.this.stopSelf() . 因此,要停止服务,您应该调用GCMIntentService.this.stopSelf() You probably don't need to do this because Android will most likely take care of it for you when you start the new Activity . 您可能不需要这样做,因为当您启动新的Activity时,Android很可能会为您处理。

NOTE: GCMBaseIntentService has been deprecated, and it's better now to use Google Cloud Messaging API . 注意: GCMBaseIntentService已被弃用,现在最好使用Google Cloud Messaging API

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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