简体   繁体   中英

Launch an activity from a different class

I am trying to launch an activity in the onPostExecute method of an AsyncTask. This async class is implemented in another class which dosent extends an activity . But I am passing application context as an argument to the method in this class. Here is the sample code

@Override
        protected void onPostExecute(String result) 
        {
            try
            {
                System.out.println("Response : "+result);
                Toast.makeText(context, "Account Successfully Created", Toast.LENGTH_LONG).show();
                Intent intent = new Intent(context,OperationPanelActivity.class);
                context.startActivity(intent);
             }
             catch(Exception e)
             {
                   Log.d("Error: ", e.getMessage());
             }
        }

Inside this class I am taking the application context like this

public void createUser(String userName,String password,String firstName,String lastName,String email,Context context)
    {
        InvokeWebService service = new InvokeWebService();
        CALL_URL = Global.SERVICE_URL+"createuserget";
        this.context = context;
        service.execute(new String[] {userName,password,firstName,lastName,email});
    }

But the new activity is not getting invoked.

How can I do this in android? Thanks

This is because your activity could be finished by the time you run onPostExecute. You should notify the activity somehow and let the activity start the new activity

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