简体   繁体   English

从其他班级发起活动

[英]Launch an activity from a different class

I am trying to launch an activity in the onPostExecute method of an AsyncTask. 我正在尝试在AsyncTask的onPostExecute方法中启动活动。 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? 我该如何在android中做到这一点? Thanks 谢谢

This is because your activity could be finished by the time you run onPostExecute. 这是因为您的活动可能会在您运行onPostExecute时完成。 You should notify the activity somehow and let the activity start the new activity 您应该以某种方式通知活动,然后让该活动开始新的活动

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

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