Android Novice
Android Novice

Reputation: 546

Android finish() Activity Not working

I got this error in my codes when i use finish(); "The method finish() is undefined for the type SaveImageTask" . Am I missing some declarations or anything. Can someone please guide me on this.

    public class SaveImageTask extends AsyncTask<String , String , String> {

            private Context context;
            private ProgressDialog pDialog;

            boolean bCancelled=false;
            public SaveImageTask(Context context) {
                this.context = context;
            }

            @Override
            protected void onPreExecute() {
                // TODO Auto-generated method stub

                super.onPreExecute();

                pDialog = new ProgressDialog(context);
                pDialog.setIndeterminate(false);
                pDialog.setCancelable(true);
                pDialog.setOnCancelListener(cancelListener);
                pDialog.show();

            }

            @Override
            protected String doInBackground(String... args) {
                // TODO Auto-generated method stub

                // MY STUFF
                return null;   
            }

            @Override
            protected void onPostExecute(String args) {
                // TODO Auto-generated method stub

                pDialog.dismiss();

            }

            OnCancelListener cancelListener=new OnCancelListener(){
                @Override
                public void onCancel(DialogInterface arg0){
                    bCancelled=true;
                    SaveImageTask.this.finish(); // <<----"The method finish() is undefined for the type SaveImageTask"
                }
            };


        }

Upvotes: 1

Views: 9525

Answers (3)

user370305
user370305

Reputation: 109257

Just use,

(Activity(context)).finish();

EDIT:

Actually you have to cast your context to Activity Context,

then you can use finish() method of Activity,

Something like,

private Activity context;

public SaveImageTask(Context context) {
                this.context = (Activity)context;
            }

now, just

context.finish();

Upvotes: 2

Vinay W
Vinay W

Reputation: 10190

 OnCancelListener cancelListener=new OnCancelListener(){
                @Override
                public void onCancel(DialogInterface arg0){
                    bCancelled=true;
                    finish(); // <<----"The method finish() is undefined for the type new DialogInterface.OnCancelListener(){}"
                }
            };

you are creating an anonymous class here. Only final variables and methods of the outer class are accessible inside an anonymous class directly. Activity 's finish is not a final method. What you need to do is reference the 'outer' class, which is an Activity and call its finish()

ActivityName.this.finish()

Note : you cannot make a static call like ActivityName.finish() hence the reference of activity's object this.

Upvotes: 2

Shine
Shine

Reputation: 3818

use SaveImageTask.this.finish();

Upvotes: 3

Related Questions