Akshit Arora
Akshit Arora

Reputation: 458

Android: Combination of AsyncTask and get()

In my android app, I have to load the data on first run through online server. But after splash screen, the loading bar should be displayed with black background. But when I am doing, it shows the content behind the progress bar. what i want is just to display the progress bar (with black background) until the data gets downloaded.

Here is my code of mainactivity.java

public class MainActivity extends Activity {



@Override
protected void onCreate(Bundle savedInstanceState) {

    task myTask = new task();

    myTask.execute();

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //other code
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    //getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

class task extends AsyncTask<String, Integer, String> {

    private ProgressDialog progressDialog;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progressDialog=new ProgressDialog(MainActivity.this, android.R.style.Theme_Translucent_NoTitleBar);
        progressDialog.setMessage("Loading...");
        progressDialog.show();
    }

    @Override
    protected String doInBackground(String... params) {
        try {
            Thread.sleep(20000);
            } catch (Exception e) {
                e.printStackTrace();
            }
        return null;
    }
    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        if(progressDialog!=null)
            progressDialog.dismiss();
    }
}
}

Well, while copy-pasting some of the brackets may be gone wrong.

But be assured the code is running fine, The problem comes that background is loaded alongwith the loading screen. I got to know about get() method to stop the process being loaded. But this also stopped the view of loading bar.

Please help. Or any alternative way suggestions?

Upvotes: 0

Views: 57

Answers (1)

Alexander
Alexander

Reputation: 48272

Remove setContentView() from onCreate() and place it in onPostExecute() at the end.

Upvotes: 1

Related Questions