I have two activities. The first one executes the second one.
Intent i = new Intent(MyOne.this, MyTwo.class);
startActivity(i);
The problem: My second activity does some heavy work on launching so it launches couple of second and before it is launched i see a black screen.
Is it any way to set a progressbar or some image instead of this black screen? Because i don't think user will wait for something, that he doesn't know. I tried setting progressbar after setcontentview in my second activity, but progressbar shows up only when activity fully started.
I suggest you to do this asynchronously using AsyncTask.
Short example:
ProgressDialog dialog;
class YourTask extends AsyncTask<Void, Void, Void> {
protected void onPreExecute() {
dialog = ProgressDialog.show(...);
}
protected Void doInBackground(Void... unused) {
try {
// doSomethingHeavy();
// publishProgress(...);
} catch(Exception e) {
//...
}
return null;
}
protected void onProgressUpdate(Void... unused) {
}
protected void onPostExecute(Void unused) {
dialog.dismiss();
}
}
You sure can. Check out the AsyncTask and this tutorial for inspiration.
Related
I have two activities. While switching to second activity by intent, it takes 3-4 seconds because it has lots of components with adapters fetching data from SQLite etc. Thus, I want to show a progress dialog while switching.
I have been digging topics for this purpose and tried many of them:
1-) Using AsyncTask on the second activity. It doesn't show the progress dialog as soon as I click on a component to switch to the second activity. It waits for 3-4 seconds and then progress dialog shows up for less then a second which is not user-friendly way.
2-) Using AsyncTask on the first activity. It shows as soon as I click on that component but the progress wheel doesn't spin. The progress dialog freezes.
3-) Using AsyncTask onStart() method on the second activity. This results as the first way.
The code below implements the second way above, using AsyncTask on the first activity.
public void toVisitRegister(Event event) { //Switching to the second activity
new startingThread().execute();
Intent toVisitRegister = new Intent(MainCalendar.this, VisitRegister.class);
startActivity(toVisitRegister);
finish();
}
And here is the AsyncTask
public class startingThread extends AsyncTask<String, String, String> {
#Override
protected void onPreExecute() {
super.onPreExecute();
startingProgress = new ProgressDialog(MainCalendar.this);
startingProgress.setTitle("Visit Register");
startingProgress.setMessage("Initializing...");
startingProgress.show();
}
#Override
protected String doInBackground(String... params) {
return null;
}
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if(MainCalendar.this.startingProgress != null) {
MainCalendar.this.startingProgress.dismiss();
}
}
}
I also tried to call startActivity in onPostExecute, but it didn't work. Therefore, I am waiting for your opinions and suggessions about this issue. Any help will be appreciated.
Thanks.
I also tried to call startActivity in onPostExecute,
Pass the Activity context to startingThread AsyncTask and put your start activity code in onPostExecute() of AsyncTask.
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if(MainCalendar.this.startingProgress != null) {
MainCalendar.this.startingProgress.dismiss();
Intent toVisitRegister = new Intent(MainCalendar.this, VisitRegister.class);
mContext.startActivity(toVisitRegister);
mContext.finish();
}
}
Here mContext is the Context of your current MainCalendar Activity.
Looking at your implementation, the AsyncTask wont have time to work because you will be jumping to the next Activity right away. Try calling the next activity in PostExecute().
I also tried to call startActivity in onPostExecute, but it didn't work.
Did you did it like this:
public void toVisitRegister(Event event) { //Switching to the second activity
new startingThread().execute();
}
public class startingThread extends AsyncTask<String, String, String> {
#Override
protected void onPreExecute() {
super.onPreExecute();
startingProgress = new ProgressDialog(MainCalendar.this);
startingProgress.setTitle("Visit Register");
startingProgress.setMessage("Initializing...");
startingProgress.show();
}
#Override
protected String doInBackground(String... params) {
return null;
}
#Override
protected void onPostExecute(String result) {
if(MainCalendar.this.startingProgress != null) {
MainCalendar.this.startingProgress.dismiss();
}
Intent toVisitRegister = new Intent(MainCalendar.this, VisitRegister.class);
startActivity(toVisitRegister);
finish();
}
}
I have a button on my app, if the user click it, it will refresh the current page by calling onResume(), and there are lots of database operations in onResume(). Instead of keeping the button stay pressed for a while, I would like to use asynctask to make a progressdialog while loading the data. But the problem is that the button will still be in pressed state and the progressdialog only show at the end of the operation for a very short duration.
RefreshButton.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
onResume();
}
});
protected void onResume()
{
doneloading = false;
monResumeloading = new onResumeloading();
monResumeloading.execute();
....loading...
doneloading = true;
}
private class onResumeloading extends AsyncTask<Integer, Integer, String>
{
private ProgressDialog progressDialog;
#Override
protected void onPostExecute(String result)
{
progressDialog.dismiss();
}
#Override
protected void onPreExecute()
{
progressDialog = new ProgressDialog(StatisticsActivity.this);
progressDialog.setMessage("Loading...");
progressDialog.setCancelable(true);
progressDialog.show();
Log.i(TAG, " doneloading=false");
}
#Override
protected void onProgressUpdate(Integer... values)
{
}
#Override
protected String doInBackground(Integer... params)
{
while(!doneloading)
{
publishProgress(0); //dummy
log.i(TAG, "loading");
}
return null;
}
}
I observed that the "loading" log is showing right after the asynctask execution and stop right after the boolean doneloading becomes false. But the progressdialog is not working properly. Please help me :(
First thing, I don't think you should be calling your AsyncTask in the onResume() function. You can simply call it from your ClickListener.
Right now, you are doing your '....loading...' code before you even execute your AsyncTask. That's why the button stays pressed while it's executing '....loading...' and then when it's done, it executes your AsyncTask which really isn't doing anything - that's why it just shows up for a short duration.
Move your '....loading...' code into your doInBackground() of your AsyncTask and it should work ok.
Summary:
Click: Execute AsyncTask
AsyncTask: opens ProgressDialog
AsyncTask: Executes your '...loading...' code
AsyncTask: Wait for '...loading...' code to complete while still displaying dialog.
AsyncTask: Dismiss ProgressDialog
I have an AsyncTask that is supposed to show a progress bar while it uploads some stuff via Internet. Sometimes it works like a charm and sometimes it does not show any progress bar. Here is the code:
public class Upload extends AsyncTask<Void, Void, Void> {
private ProgressDialog dialog = new ProgressDialog(Activity.this);
protected void onPreExecute() {
dialog = ProgressDialog.show(Activity.this, "wait...", "", true, true);
}
#Override
protected Void doInBackground(Void... params) {
//upload stuff
return null;
}
protected void onPostExecute(Void result) {
try {
if (dialog.isShowing())
dialog.dismiss();
dialog = null;
} catch (Exception e) {
// nothing
}
Intent next = new Intent(getApplicationContext(), SecondActivity.class);
startActivity(next);
}
}
}
The doInBackground and onPostExecute work always, and sometimes altogether it works like a charm. But sometimes, there is no progress bar while it is uploading. Is this a race condition? I do not think so, but I cannot find any explanation.
You're creating the object twice in the class. The ProgressDialog.show already returns a created ProgressDialog object, but you have instantiated it first at the top. The ProgressDialog should be instantiated once, so try removing the instantiation at the top and try again, like so:
private ProgressDialog dialog;
protected void onPreExecute() {
dialog = ProgressDialog.show(Activity.this, "wait...", "", true, true);
}
Maybe it is because void parameter that causes that problem. Just try to use Integer as your parameters.:
public class Upload extends AsyncTask<Integer, Integer, Integer>
in my app, i am switching one activity to another activity (Main.java to Feature_Screen.java). In the Feature_Screen(second activity) i am going to download large no of data and image to set in a grid view. so that i use Async Task for download it. although i use async task in second activity i get black screen while switching Main.java to Feature.java. i search in google but all the answers says use Async Task.
example coding:
public class Main extends TabActivity{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.tabbar);
.................
intent = new Intent().setClass(this, Featured_Screen1.class);
spec = tabHost.newTabSpec("home").setIndicator("",
res.getDrawable(R.drawable.top_book_icon)).setContent(intent);
tabHost.addTab(spec);
...........
}
}
In Feature_Screen1.java(Second.java):
public class Feature_Screen1 extends Activity{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.feature);
.................
new Content_load().execute();
...........
}
class Content_load extends AsyncTask<Void, Void, Void>
{
ProgressDialog dialog = new ProgressDialog(SignInPage.this);
protected void onPreExecute() {
dialog.setMessage("Please wait...");
dialog.setCancelable(false);
dialog.show();
}
protected void onPostExecute(Void unused) {
dialog.dismiss();
}
protected Void doInBackground(Void... arg0) {
..........
return null;
}
}
}
my problem is how to avoid black screen while switching between activities? please help me.
Finally I found the answer for black screen problem..
Use AsyncTask like below, this worked for me, if you face any problems let me know
public class SyncroniseRecords extends AsyncTask<Void, Void, Void>
{
#Override
protected void onPreExecute()
{
super.onPreExecute();
dialog.setMessage("Please wait...");
dialog.setCancelable(false);
dialog.show();
}
#Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
// Things to be done while execution of long running operation is in progress. For example updating ProgessDialog
}
#Override
protected void onPostExecute(Void result)
{
dialog.cancel();
}
#Override
protected Void doInBackground(Void... params) {
return null;
}
}
This will launch the Activity without any Black Screen. Using onPreExecute you can display the Progressbar.Once the process gets completed you can cancel it in OnPostExecute().
I am working on an android app, in that app i have intent2 which on click redirects to intent3 and takes some time then loads a table and displays server data into it.
Sometimes if there is a lot of data, it tales pretty much time to get the dataload and the time blank screen is displayed increases.
i wish to show a loading bar till the data loads.
how can i show the ProgrssBar till only when data is not displayed ?
Probably your best bet would be to use AsyncTask in your "intent3":
You could do it like this:
private class performBackgroundTask extends AsyncTask <Void, Void, Void>
{
private ProgressDialog Dialog = new ProgressDialog(ClassName.this);
protected void onPreExecute()
{
Dialog.setMessage("Please wait...");
Dialog.show();
}
protected void onPostExecute(Void unused)
{
try
{
if(Dialog.isShowing())
{
Dialog.dismiss();
}
// do your Display and data setting operation here
}
catch(Exception e)
{
}
#Override
protected Void doInBackground(Void... params)
{
// Do your background data fetching here
return null;
}
}
You probably need to run an AsyncTask on onCreate when you open the new activity, the structure of the asynctask would be like this (taken from the google doc), notice that if you want to increament a progress bar you have to implement onProgressUpdate and call publishProgress in the doInBackground method
private class DownloadFilesTask extends AsyncTask<Void, Integer, Void> {
protected void onPreExecute()
{
// show your progress bar
}
protected Void doInBackground(Void... params) {
// do your work and publish the progress
publishProgress(progress);
}
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
protected void onPostExecute(Void result) {
//dismiss your progress bar
}
}
This code is just an example, of course you need to adapt it to your logic/code.
Check out this simple and complete example