2. 3. Addthe followingcode in onCreate functionof yourActivity“ActivityLifeCycleAndActivities”
// intilization
btnActTwo = (Button) findViewById(R.id.btnActTwo);
btnContact= (Button) findViewById(R.id.buttonContact);
btnColor= (Button) findViewById(R.id.buttonColor);
container= (LinearLayout) findViewById(R.id.container);
// link UI elements to action the code
btnContact.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
btnColor.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
btnActTwo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
4. Run and Install the app and you should see the similar output as shown below.
3. 5. Type the following line in the onCreatefunction of your current Activity.
6. Write the following code after the function of “OnCreate () as follows
And then press the key “ctrl+o” ( Note, this only works in Android Sudio) and choose the method OnRestart
as shown in the snapshots below.
4.
5. After step 6, your code will look like this.
7. After that add all functions of Activity Life cycle using the step 6. As shown below
@Override
protected void onRestart() {
super.onRestart();
}
@Override
protected void onStart() {
super.onStart();
Log.d(TAG, "MainActivity: onStart()");
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "MainActivity: onResume()");
6. }
@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "MainActivity: onPause()");
}
@Override
protected void onStop() {
super.onStop();
Log.d(TAG, "MainActivity: onStop()");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "MainActivity: onDestroy()");
}
8. Add the following line in the global space of your activity (it is the space write after your activity)
9. Now at least run the emulator and device first and click the “logcat” and choose “edit filter configuration”.
10. Add the following in the filter “states”.
7. 11. You shouldsee the followinglinesinthe logcat afterrunningyourapplication
Part 2. Creating Second Activity
1. Right click on your package folder as shown below and add a new Activity and name it
as “ActivityTwo”.
8. 2. Also add the xml file for “ActivityTwo” and name it “activitytwo” by choosing the
following actions.
9. 3. Add the following code in the “activitytwo.xml”.
4. <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is Activity Two">
</TextView>
</LinearLayout>
5. Go to the source folder of your Activity and add the following code.
public class ActivityTwo extends Activity {
final String TAG = "States";
@Override
public void onCreate(Bundle savedInstanceState) {
10. super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
Log.d(TAG, "ActivityTwo: onCreate()");
}
@Override
protected void onRestart() {
super.onRestart();
Log.d(TAG, "ActivityTwo: onRestart()");
}
@Override
protected void onStart() {
super.onStart();
Log.d(TAG, "ActivityTwo: onStart()");
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "ActivityTwo: onResume()");
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "ActivityTwo: onPause()");
}
@Override
protected void onStop() {
super.onStop();
Log.d(TAG, "ActivityTwo: onStop()");
}
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "ActivityTwo: onDestroy()");
}
}
6. Remove the errors if any
7. Now Go to the activity “ActivityLifeCycleAndActivities”andaddthe followingcode inthe
listener.
8. //Button Listener for Activity Two
btnActTwo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,
ActivityTwo.class);
startActivity(intent);
}
});
11. 9. Now Run the Application and press the button “Go To Activity Two”. And you should
see the following. ( If not, remove the logical errors by debugging)
In the log cat.
Part 3. Using onActivityFor Result
1. Add new activity as “ActivityThree” ( see step 1 on Part 2)
2. Add xml file in the layout folder of your project as “activitythree” and add the following
code.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
13. 7. Now go to the Activity “ActivityLifeCycleAndActivities” andaddthe followingcode inthe
listener.
btnColor.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, ActivityThree.class);
startActivityForResult(intent, PICK_COLOR );
}
});
8. Add the “PICK_COLOR” variable in the global space of this Activity.
9. Add the following function after the On Destroy function. Of this activity.
14. public void changeBackground(String color) {
// depending the extra value String, choose a background color
if (color.equals("red")) {
container.setBackgroundColor(Color.RED);
} else if (color.equals("green")) {
container.setBackgroundColor(Color.GREEN);
} else if (color.equals("blue")) {
container.setBackgroundColor(Color.BLUE);
}
}
10. Add the following line of code in the same Activity. ( Just write few onActi)
15. 11. Remove the Error if any.Run your application and pressthe button“CHANGECOLOR”andselect
Redfrom the ActivityThree andyoushouldsee yourbackgroundredinthe firstactivity.
16.
17. Part 4 showing the Contacts of your
smartphones
1. Add the following code in the listener of your activity “ActivityLifeCycleAndActivities”
2. add the pickContact as shown.
3. copy and paste the following code in the “onActivityResult” Method.
if ( requestCode==PICK_COLOR)
{
String clr=data.getStringExtra("COLOR");
changeBackground(clr);
}
// Check which request it is that we're responding to
else if (requestCode == PICK_CONTACT_REQUEST) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
// Get the URI that points to the selected contact
Uri contactUri = data.getData();
// We only need the NUMBER column, because there will be only one row in
the result
String[] projection = {ContactsContract.CommonDataKinds.Phone.NUMBER};
// Perform the query on the contact to get the NUMBER column
// We don't need a selection or sort order (there's only one result for the
given URI)
18. // CAUTION: The query() method should be called from a separate thread to
avoid blocking
// your app's UI thread. (For simplicity of the sample, this code doesn't
do that.)
// Consider using CursorLoader to perform the query.
Cursor cursor = getContentResolver()
.query(contactUri, projection, null, null, null);
cursor.moveToFirst();
// Retrieve the phone number from the NUMBER column
int column =
cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String number = cursor.getString(column);
// Do something with the phone number...
}
}
4. Run the application , you should see the output like this. After pressing the
“contactView” Button.