How can you generate a loading screen in Android?
Asked Answered
S

2

5

The problem is I am coming from another Activity, and when I try to go to the new Activity, it just sits on the old one until the new one is displayed, so I am trying to get it to go to the new Activity right away, and then bring up a loading screen while it gets the content. (The content is either coming from a website or an internal database).

I've tried the progressDialog from the Android development site but it doesn't do anything as the Activity finishes loading before showing anything, so by time it shows up, theres nothing to load.

Sunstroke answered 3/11, 2011 at 3:53 Comment(0)
S
14

First start new activity first and then call the async task file.. this will start new activity when u close old one. in Oncreate of new activity call the asyn task class like below

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
            setContentView(your layout here);
            new GetTask(this).execute();

   }
}

class GetTask extends AsyncTask<Object, Void, String> {
    Context context;

    GetTask(Context context, String userid) {
        this.context = context;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        mDialog = new ProgressDialog(mContext);
        mDialog.setMessage("Please wait...");
        mDialog.show();
    }

    @Override
    protected String doInBackground(Object... params) {
        // here you can get the details from db or web and fetch it..
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        mDialog.dismiss();
    }
}
Sallee answered 3/11, 2011 at 4:14 Comment(1)
This AsyncTask worked wonders, I just applied some generic content on the first list item while it loaded and then retrieved it from the actual generated content in the postExecuteSunstroke
R
0

As a suggestion, try reducing the code in onCreate method of the second activity. Let it be as simple as calling super and setting the content views. This will bring up the UI as designed in XML.

Move rest of the code to your onResume method. Next step, if you have some data to be fetched from DB or outside, try using the thread (how to do it & what is it) or async task.

Russian answered 3/11, 2011 at 4:4 Comment(2)
Well I use a few startActivityForResults, will that cause it to reload on each time it returns?Sunstroke
no, if you use startActivtyForResult, you need to implement onActivtyResult(..) and the context returns from here.. It will not reload the activityRussian

© 2022 - 2024 — McMap. All rights reserved.