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
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!