Android onCreate loading delay - android

My app takes a while to initiate (MainActivity), so I want a separate thread to show a loading indicator for 10 seconds (ignore all other touch events within this 10 seconds) then disappear automatically. How do I do this?

If your main activity takes several seconds to initialize, then the initialization is what should be on a separate thread, not the splash screen. You should never block the UI thread with time-consuming operations.
You can organize your initialization something like this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set up the splash screen
setContentView(R.layout.splash_screen);
// set up and start the initialization thread
final Handler handler = new Handler();
new Thread() {
public void run() {
// Do time-consuming initialization.
// When done:
handler.post(new Runnable() {
public void run() {
// set up the real UI
}
});
}
}.start();
}
That will remove the splash screen and replace it with the real UI after the time-consuming initialization is finished.
If you always want to wait a minimum of 10 seconds, you can record the start time in a local variable before starting the thread and then after initialization is finished, if there is still time left you can use postDelayed or postAtTime.
The above code uses a Handler and a Thread because what you want to do is fairly straightforward. As an alternative, you could use an AsyncTask, which does essentially the same thing. It also has built-in tools that allow you to "publish" initialization progress to the UI thread. See the docs for details.

Cover the Main Activity with splash screen(Any edge to edge image will do).
Display a progress bar using Progress Bar
Disable touch Events for the Splash screen so that the touch event doesn't pass towards the main activity screen.
Remove the Splash Screen from view when the loading is done in background or after a specific time.
Benefits:
No handlers/threads required because you stay in the main activity the whole time.
Updating progress bar will be a breeze because you stay in the UI thread the whole time.
Application less likely to crash because touch events are disabled during loading so no burden on UI thread.

Related

Indeterminate Progress Bar only shows very briefly at the end of the operation

I want it to show up as soon as I click the login button, but it only shows after the loggin process has finished
What am i doing wrong
runOnUiThread(new Runnable() {
public void run() {
mProgress.setVisibility(View.VISIBLE);
}
});
doLogin();
mProgress.setVisibility(View.INVISIBLE);
Since you are using another thread to show the Visibility, the runnable might get scheduled any time in the future. That is the reason it is being shown late.
Maybe adding a sleep routine in current thread immediately after runonuithread call might schedule the runnable, but you can never be really sure.
Edit: Or you could create a Handler which shows Progress bar and wait for it to be shown and then continue
The reason You don't see the loading dialog is that you call a blocking function in your GUI thread/MAIN thread.When you set your dialog to visible you should let the GUI thread to keep running so it can draw the dialog on the screen.Moreover after 5 seconds that the GUI thread is blocked, the user will be presented with FORCE CLOSE/Wait pop up.
It is a bad practice to preform long operation of the gui thread, you MUST only can non blocking function in it.
Here is how you should do it:
//make sure mProgress is final
mProgress.setVisibility(View.VISIBLE);
new Thread(){
public void run(){
doLogin();
mProgress.setVisibility(View.INVISIBLE);
}
}.start();

Android - show progress dialog while doing work on UI thread

I need to do some work on the UI thread, specifically setting up some views, etc. - this can't be done in a background thread. The process is invoked on a button click and takes about a second or so to complete - without a progress dialog it looks as if the app is frozen. I use progress dialog with AsynTasks in several places and it works fine - however here I'm struggling.
I started with simple:
showDialog(DIALOG_PLEASE_WAIT);
viewInfo.setFromGuide(true); //this method just sets a variable
viewInfo.setVenue(venue); //this method does a lot of UI manipulation and takes a second or so
showScreen(VIEW_INFO); //this method shows the corresponding view in ViewFlipper
dismissDialog(DIALOG_PLEASE_WAIT);
However the dialog would not show (sort of expected, as this is all on UI thread.
Then I changed the code to this:
Handler hnd = new Handler() {
#Override
handleMessage(Message m) {
viewInfo.setFromGuide(true);
viewInfo.setVenue(venue);
showScreen(VIEW_INFO);
dismissDialog(DIALOG_PLEASE_WAIT);
}
}
showDialog(DIALOG_PLEASE_WAIT);
new Thread() {
public void run() {
hnd.sendEmptyMessage(0);
}
}.start();
This still doesn't show the dialog - naturally, the UI work in handleMessage is still done on the UI thread. So, what can I do to show the progress dialog?
If it takes really takes a second or so to complete than maybe you can just use a simple Toast notification with a message like "Please wait"
as you are using AsyncTask you can override onProgressUpdate which is called when ever you call publishProgress() from inside the doInBackGround so you can publish your results smoothly while working in background because, onProgressUpdate works on the UI thread.

How can I make my app launch faster?

I am experimenting with an app I am developping.
When I launch the app, there is currently a 3 second delay before the app UI is usable. During the delay the screen is black, apart from the task bar and, below it, the app title bar.
I was thinking about displaying a splashscreen as a dialog in the main Activity. However, it is only displayed after those 3 seconds, which makes it useless. This means that nearly all of the 3-second delay takes place between the launch and the call to
super.onCreate(savedInstanceState).
Can anyone educate me on what is happening behing the scenes during this delay ? Is there anything I can do to shorten it ?
Try to locate the slow code and put it into a second thread.
new Thread(new Runnable() {
#Override
public void run() {
// slow code goes here.
}
}).start();

how to stop program while alertDialog open?

I want stop main thread, while alertDialog is open, and wait user's choice for continue the program (depending on the button pressed buttons (positive or negative)). When trying to stop thread after. show (); dialog is not draw; if hang listener's onDestract onClose it works, but the code is horrible.
You can't stop "main" thread, since it's gui thread - your alertDialog will become irresponsive. You need to use callback to perform desired operations.
Why you need to stop main thread? Is this some game?
You must use many thread, and never block main thread. For example, thread for logic, thread for draw.
If you need to stop drawing, simple set your noDraw=true property:)
Like this:
SomeDrawer extends View {
#override
public void onDraw(...){
if(noDraw) return;
redrawMyGame(...);
}
}

Intentionally force android to wait AND load something in background

I want to force android to wait AND continue processing something at the same time. I have seen the Thread wait function, but that just makes things hang for a while not actually letting the app do anything. Subsequent processes are simply queued up waiting their turn.
I want to force the timing of a process. This is kind of a combination between having a thread with a wait AND an asynctask
insight appreciated
public class yourActivity extends Activity{
final WebView yourWebview; //this is the webview
Context mContext = this;
public void onCreate(Bundle B){
setContentView(R.id.somethingtoshow);//this will be shown while webview working
Runnable yourRun = new Runnable(){
public void run(){
yourWebview = new WebView(mContext);
//do whatever you want with it
//loadUrl and whatever you want
//when your done
runOnUiThread(new Runnable(){
public void run(){
setContentView(yourWebView);
}
});
}
};
Thread T= new Thread(yourRun);
T.start();
}
}
'Waiting' means to put the thread in a suspended state - do you mean having the app simply do nothing until the process is completed?
You never want to make the main event thread hang or wait, that will make the user think the app is frozen. To do what you are wanting, you will probably spawn an async thread that loads the page from the main activity. The activity will continue to display whatever you had it doing last, and will not hang up or freeze while the async is going in the background. However, the user will still be able to press buttons, and might mess you up.
So to get the app to appear unfrozen and allow a process to occur in the background, you will want to enter into some loading screen or limit the user's options on the main layout. This will allow activity to continue occurring but allow the user a smooth experience.

Categories

Resources