开发者

Asynk task android

I have a tabgroup having multiple activities. In one of the tabs i have two activities between whom i want to place a progress dialog.For this i am using Asynk Task. Following is my AsynkTask class which i have made an inner class for AboutUs activity:

private class TheTask extends AsyncTask<Void, Void, Void>{

    @Override
    protected void onPreExecute() {
        progDialog = ProgressDialog.show(AboutUs.this.getParent(), "Loading... ",
                "please wait....", true);
    }

    @Override
    protected Void doInBackground(Void... params) {
        final Intent aboutusIntent = new Intent(getParent(), Departments.class);
        final TabGroupActivity parentActivity = (TabGroupActivity)getParent开发者_开发问答();
        parentActivity.startChildActivity("Departments", aboutusIntent);
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {

        if(progDialog.isShowing())
        {
        progDialog.dismiss();
        }
    }
}  

I am calling this class in my AboutUs activity :

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.aboutus);
         .  
         .  
         .  
         .  
/* Button for going to Departments */
    Button ourdepbtn = (Button) findViewById(R.id.departmentsbutton);
    ourdepbtn.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            //ourDepartments();
            new TheTask().execute();
            return false;
        }
    });
}  

However this does'nt start a new activity i.e. Departments. The progress dialog appears and then disappears but activity never loads.

Any suggestions..??


First, you cannot start an activity from a non GUI thread (which Async doInBackground() is). Just start directly inside your Button.onClick() (why you use onTouch?) listener.

If you want to show up a ProgressDialog for the new Activity as soon as possible, you need to create it in the new (child) Activity onCreate(), as your ProgressDialog is connected to the new (child) activity (is it?). Take care about the order of creating layouts (create the ProgressDialog after calling setContentView()). I am not very sure why you want to show that ProgressDialog. Is there something which delays the display of the childActivity? You loading some data? Then, the Dialog should be related to that loading task (Async I guess).


private class TheTask extends AsyncTask{

    Context con;
     Intent aboutusIntent;
     TabGroupActivity parentActivity;
    private TheTask(Context context)
    {
        this.con=context;
    }

    @Override
    protected void onPreExecute() {
        progDialog = ProgressDialog.show(con, "Loading... ",
                "please wait....", true);
    }

    @Override
    protected Void doInBackground(Void... params) {
         aboutusIntent = new Intent(con, Departments.class);
          parentActivity = (TabGroupActivity)getParent();
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        if(progDialog.isShowing())
        {
        progDialog.dismiss();
        }
        parentActivity.startChildActivity("Departments", aboutusIntent);

    }
}  

Thanks for your suggestions Oliver :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜