Results 1 to 12 of 12
  1. #1
    AndreBernard is offline Member
    Join Date
    May 2014
    Posts
    6
    Rep Power
    0

    Default Why my TOAST not appear?

    Hello,

    I'm newer in ANDROID and not understand why the TOAST not appear in this case.
    If i comment the loop "Boucle()" that's works, else not

    Java Code:
    package com.example.toast;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Gravity;
    import android.widget.Toast;
    
    public class MainActivity extends Activity {
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) 
    	{
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    		
    		Toast toast= Toast.makeText(MainActivity.this, "I'm a TOAST and i not appear.", Toast.LENGTH_LONG);  
    		toast.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL, 0, 0);
    		toast.show();
    		
    		Boucle();
    	}
    	
    
    	public void Boucle() 
    	{
    		
    		Integer a = 0;
    		
    		for(int i = 1; i <= 500000; i++)
    		{
    			
    			a++;
    
    		}
    		
    	}
    
    }
    Thanks and good day
    Last edited by AndreBernard; 05-27-2014 at 04:40 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Why my TOAST not appear?

    Why that long running loop? It keeps the jvm from doing anything.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    AndreBernard is offline Member
    Join Date
    May 2014
    Posts
    6
    Rep Power
    0

    Default Re: Why my TOAST not appear?

    Hello NORM,

    In fact, i have reproduce the problem by a simple example.
    In my original code, the loop is an enumeration of the numerous files inside a folder and writing the result in a TXT file.

    I search to show the toast before, for say to the user he must wait while the enumeration is not finish
    But impossible to show this TOAST.

    In this code it's exactely the same result.
    I have even execute an Asynchrone task for enumerate the file, and it's always the same result, the toast appear only after the thread of enumeration is finish

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Why my TOAST not appear?

    toast appear only after the thread
    Sounds like the thread is holding up something.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Why my TOAST not appear?

    Quote Originally Posted by Norm View Post
    Sounds like the thread is holding up something.
    Not surprising because that loop as shown is running in the main thread which, in Android, is the thread controlling events and painting (IIRC).

    The OP needs to launch some form of worker thread. The "how" of that should be easy enough to find in the Android docs.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    AndreBernard is offline Member
    Join Date
    May 2014
    Posts
    6
    Rep Power
    0

    Default Re: Why my TOAST not appear?

    Hello TOLLS,
    The solution is to create another activity, just for one TOAST ??
    There are not more simple way ??
    Last edited by AndreBernard; 05-27-2014 at 06:01 PM.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Why my TOAST not appear?

    Not an activity for the toast.
    A thread for the looping code that you have.

    The basic idea with Android code is to not do anything major on the main thread, but to spawn a new thread to do the work. In your case the work is this looping over folders (or something).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    AndreBernard is offline Member
    Join Date
    May 2014
    Posts
    6
    Rep Power
    0

    Default Re: Why my TOAST not appear?

    Aaaah ok !!! I believe i have understand.
    I must create another activity and do all i need in it, the ListView, the button, the enumeration of file, etc...
    The main activity must do the minimum for can show the toast

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Why my TOAST not appear?

    No. It's not another Activity.
    Activities run on the main (UI) thread (if they're part of the same app/process), so another Activity will make no difference.

    You want a worker thread.

    Read about threads here, from the Android docs I have already referred you to.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    AndreBernard is offline Member
    Join Date
    May 2014
    Posts
    6
    Rep Power
    0

    Default Re: Why my TOAST not appear?

    Thanks to you, this time i have understand the difference between activity and thread

    Usually i use AsyncTask
    Java Code:
    new DownloadImageTask().execute("http://example.com/image.png");
    
    private class DownloadImageTask extends AsyncTask<String, Void, Bitmap>
    ....
    Am i forced to use AsyncTask or just Worker threads is enough for enumerate a big folder ?
    Java Code:
     new Thread(new Runnable() {
            public void run() {
                final Bitmap bitmap = loadImageFromNetwork("http://example.com/image.png");
                mImageView.post(new Runnable() {
                    public void run() {
                        mImageView.setImageBitmap(bitmap);
                    }
                });
            }
        }).start();

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Why my TOAST not appear?

    I've only ever used Tasks.
    That doesn't mean that's correct, though.
    You need to go through that page and see how they handle jobs like yours.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  12. #12
    AndreBernard is offline Member
    Join Date
    May 2014
    Posts
    6
    Rep Power
    0

    Default Re: Why my TOAST not appear?

    Thanks for all your precious advice

Similar Threads

  1. toast wont work in time
    By nour in forum Android
    Replies: 5
    Last Post: 04-11-2012, 10:34 PM
  2. toast crashing my app
    By Drun in forum Android
    Replies: 2
    Last Post: 01-16-2012, 05:07 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •