Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9169909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:56:37+00:00 2026-06-17T15:56:37+00:00

I would like to know when into an Activity Android execute a function itself

  • 0

I would like to know when into an Activity Android execute a function itself and when is necessary to call a function. For example in the following script I downloaded, the first 4 methods are executed without calling it, but the last one sendMessage(), needs to be called:

public class BroadcastChat extends Activity {
    // Debugging
    private static final String TAG = "BcastChat";
    private static final boolean D = true;

    // Message types sent from the BluetoothChatService Handler
    public static final int MESSAGE_READ    = 1;
    public static final int MESSAGE_WRITE   = 2;
    public static final int MESSAGE_TOAST   = 3;

    // Key names received from the BroadcastChatService Handler
    public static final String TOAST = "toast";

    // Layout Views
    private ListView    mConversationView;
    private EditText    mOutEditText;
    private Button      mSendButton;

    // Array adapter for the conversation thread
    private ArrayAdapter<String> mConversationArrayAdapter;
    // String buffer for outgoing messages
    private StringBuffer mOutStringBuffer;
    // Member object for the chat services
    private BroadcastChatService mChatService = null;


    // The Handler that gets information back from the BluetoothChatService
    private final Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {

            if(D) Log.e(TAG, "[handleMessage !!!!!!!!!!!! ]");

            switch (msg.what) {

                case MESSAGE_WRITE:

                    byte[] writeBuf = (byte[]) msg.obj;
                    // construct a string from the buffer
                    String writeMessage = new String(writeBuf);
                    mConversationArrayAdapter.add("Me:  " + writeMessage);
                    break;
                case MESSAGE_READ:
                    String readBuf = (String) msg.obj;
                    mConversationArrayAdapter.add("You:  " + readBuf);
                    break;               
                case MESSAGE_TOAST:
                    Toast.makeText(getApplicationContext(), msg.getData().getString(TOAST),
                                   Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };    


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(D) Log.e(TAG, "+++ ON CREATE +++");

        // Set up the window layout
        setContentView(R.layout.main);
    }


    public void onStart() {
        super.onStart();
        if(D) Log.e(TAG, "++ ON START ++");

        setupChat();
    }

    @Override
    public synchronized void onResume() {
        super.onResume();
        if(D) Log.e(TAG, "+ ON RESUME +");

        mChatService.start();
    }


    private void sendMessage(String message) {

    if(D) Log.e(TAG, "[sendMessage]");

    // Check that there's actually something to send
    if (message.length() > 0 ) {
        // Get the message bytes and tell the BluetoothChatService to write
        byte[] send = message.getBytes();
        mChatService.write(send);

        // Reset out string buffer to zero and clear the edit text field
        mOutStringBuffer.setLength(0);
        mOutEditText.setText(mOutStringBuffer);

    }
}

    ... Incomplete script, just a part shown for the question

}

So my question is double:

1- In an Android Activity are the methods called sequencially from the first line to the last one?, Is there a loop that makes the “pointer” go back to the first line once the last one is reached?

2-How can you determine which methods are going to be executed automatically (like onCreate() ) and which are going to wait until they are called by another method of the script.

Thank you very much for your time

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T15:56:38+00:00Added an answer on June 17, 2026 at 3:56 pm

    The first thing to understand, and it’s a vital point, is that this is not a script, it’s code. If you think of it as a script, you won’t “get” what the code is doing. A script executes from start to finish. It might branch out into a function but ultimately, things happen in order.

    In Java ( and therefore Android), everything happens as a response to an event or a callback. Some of these events are raised by Android and Nickolaus has already pointed you to the Activity lifecycle which documents callbacks made by Android to your Activity and the precise order in which they happen. Other events are raised by Receivers, ContentProviders, Listeners etc.

    Note that this order is not time based (although of course you can create time based code events) and doesn’t happen one after another. They are called when the state of the Activity changes, and only when the state changes.

    In the handlers for these callbacks, you can of course call your own functions, create instances of classes and call their methods, and do stuff in order, from top to bottom – but only inside the handler.

    The first thing that happens when your app starts is that Android instantiates the Application class. Every app has an instance of the Application class, whether you know it or not, and that Application class instance also has a lifecycle similar to an Activity, so Application.onCreate() is the first event in the application to be fired. Once the Application class instance is instantiated, then the main activity, defined in you rmanifest, is created and it’s onCreate() method is called.

    After that, everything happens in response to a callback from, for example listeners (onClick, onReceive etc) or in response to events. From the end of your onCreate(), your code only executes when some other event happens.

    You can shorten all this, and answer your question,by saying that sendMessage can only be called from somewhere inside a callback handler.

    It gets more complicated when there are multiple threads executing code but that’s for another day.

    I hope that this helps rather than makes things more confusing!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to know following: I added the folder Graphics into my project
I would like to know how to enter commands into the cmd.exe (command prompt
I would like to know how to convert SQL query listed below into LINQ
I would like to know how to load an external Javascript into my document
I am interested into getting into bash scripting and would like to know how
I would like to split a line into words. I know this can be
I would like to dive into the world of SharePoint, but don't really know
I would like to know how to retrieve some new activity like the stackoverflow
I would like to know how to implement the result of the following code
I would like to know how I can import a library into Objective-C at

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.