All exams are online now due to COVID related precautions | Download the app or write to us at

Intents and Activities in Android

Learning Resources

Intents and Activities in Android

The android UI is built around the concept of Activities. Each screen you see when you use an android app is an activity. And as usual, each activity has a layout that in programming methodology can only be accessed by the thread which set/created the layout. Android however has better method for creating activities and getting back information from them than purely language oriented methods (like global variables, shared objects etc). Android defines the notion of an intent, which can be used to give some information to an Activity and also get back information from an activity as an intent. The general code flow looks like the following :

create an intent and start the activity :

 final int result = 1;
Intent intent = new Intent(VoipChat.this, UserSettings.class);
startActivityForResult(intent, result);

Add code in the event handler when an activity returns ( you need to check for the specific return code which is the same as result)

   public void onActivityResult(int requestCode, int resultCode, Intent data) {
                 if (resultCode == Activity.RESULT_OK && requestCode == 1) {
                                    //Do some work here

In the activity class which is started, make sure you put the necessary data in a new intent before calling the finish() method. The code in for example would look something like :

   Bundle bundle = new Bundle();
Log.d("USERSETTINGS", "Returning : "+userName.getText().toString());
bundle.putString("UserName", userName.getText().toString());
this.setResult(RESULT_OK, data);

Accordingly the code inside the if block above would look something like :
   String result = data.getExtras().getString("UserName");

note that data is the intent variable in the arguments for the onActivityResult() method.