Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
397 views
in Technique[技术] by (71.8m points)

android - Finish the calling activity when AsyncTask completes

My calling activity:

public class Hello extends Activity {  

public void onCreate(Bundle savedInstanceState) {

    MyTask mt = new MyTask(this);
    mt.execute();
}

Now In MyTask (an external class):

public class MyTask extends AsyncTask<Void, Void, Void> {
private Context mContext;

public MyTask(Context context) {

    mContext = context;
}  

//doinbackground, etc

    protected void onPostExecute() {
    mContext.finish();

}

Other things are working as expected if I remove mContext.finish() above.
But if I'm calling mContext.finish() , I'm getting an error: The method finish() is undefined for the type Context (Eclipse doesn't show finish() when I write mContext. so that suggests I'm using finish() wrongly.)

What do I need to do to finish the calling activity Hello after MyTask completes the task

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
((Activity)mContext).finish();

Would be the correct way to cast a Context to an Activity and call its finish() method. Not sure why you'd want to finish an Activity from an AsyncTask though


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...