简体   繁体   中英

Get back to Android activity after background thread is complete

I'm using Parse with my Android app, and my code looks like this:

public class SignupActivity extends Activity {
//Collect signup data
User.signupInBackground(data);
}



public class User{
//User methods, constructors, etc
public static void signup(data){
ParseUser pUser = new ParseUser();
//Build data into pUser
pUser.signUpInBackground(new SignUpCallback() {
        public void done(ParseException e){
            if (e!=null){
                Log.v("Signup",e.toString());
            }
        }

    });

So the question is, how do I notify my activity when the signUpInBackground process is complete? I can't have SignupActivity implement SignUpCallback because it's an abstract class and I have to extend Activity.

Ultimately, what I'm trying to do is display a dialog box or waiting animation, and then get rid of it when the background thread is done. Alternatively, the background thread could launch an activity. The problem with this is that the User class and the anonymous inner class don't have their own Context, so they can't start activities.

I'm fairly new at this, so thanks for your help.

Several approaches might work given your current code structure.

  1. Create a Handler in SignupActivity and pass that to the User so it has a way of interacting with the activity.
  2. Make SignUpCallback an interface instead of an abstract class.
  3. Create an instance of a concreate subclass of SignUpCallback in your SignupActivity class; it will have access to the methods of SignupActivity .

I'm assuming that signUpInBackground is executing on a worker thread and that the callback is invoked from that thread. If that's correct, then in all cases you will need to interact with SignupActivity through a Handler . Thus, I'd suggest method #1 unless the other approaches allow for cleaner code.

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