Search This Blog

Thursday 14 June 2012

Android Sample : start a child activity from main activity

Android Sample : start a child activity from main activity

The sample application will explain that how to start the child activity from the main activity.

Create an android project in eclipse. The main activity will be create by default by the wizard.

Modify the default activity class as follows

Android Sample - ActivitySample: ActivitySample is an android sample for explaining the android activity life cycle.
Step 1 : Create a android project in eclipse with any name or “ActivitySample”.
Once the sample created explore the project src and open the activity class


The main activity class is generated by wizard for the application. Add the highlighted code.

package com.creativeandroidapps.SampleActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class ActivitySampleActivity extends Activity {
    /** Called when the activity is first created. */
    private static final String TAG = "MainActivity";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Log.i(TAG, "Activity Life Cycle : onCreate : Activity Created");
    }
}


Now add the following life cycle methods


@Override
    protected void onStart() {
        super.onStart();
        Log.i(TAG,"Activity Life Cycle : onStart : Activity Started");
        
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i(TAG,"Activity Life Cycle : onResume : Activity Resumed");
    }
    @Override
    protected void onPause() {
        super.onPause();
        Log.i(TAG,"Activity Life Cycle : onPause : Activity Paused");
    }
    @Override
    protected void onStop() {
        super.onStop();
        Log.i(TAG,"Activity Life Cycle : onStop : Activity Stoped");
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i(TAG,"Activity Life Cycle : onDestroy : Activity Destroyed");
    }




open the res->layout->main.xml file and add a button in the layout view



   <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" 
        android:onClick="onPlayButton"/>

open the main activity file and add the function for the button

    public void onPlayButton(View view)  
    {  
        Intent done = new Intent(ActivitySampleActivity.this, ChildActivity.class);
        startActivity(done);
        Log.i(TAG,"Button Pressed : Starting Child Activity");
    }

The Intent will start the child activity.

Now add a new layout for the child activity in res->layout. Right click and add new android layout file.

Add a XML layout for Activity in Android
Add a Layout File

open the xml file or graphical layout and add the following layout


<?xml version="1.0" encoding="utf-8"?>
<RatingBar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</RatingBar>

This is just adding a rating bar


Now add a class ChildActivity by right click on the package in the Package Explorer.

Add a JAVA Class in eclipse
Add a JAVA class

open the class file and add the following code



import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class ChildActivity extends Activity {

    private static final String TAG = "ChildActivity";
      /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.child);
        Log.i(TAG,"Activity Life Cycle : onCreate : Activity Created");
    }
    
    @Override
    protected void onStart() {
        super.onStart();
        Log.i(TAG,"Activity Life Cycle : onStart : Activity Started");
        
    }
    @Override
    protected void onResume() {
        super.onResume();
        Log.i(TAG,"Activity Life Cycle : onResume : Activity Resumed");
    }
    @Override
    protected void onPause() {
        super.onPause();
        Log.i(TAG,"Activity Life Cycle : onPause : Activity Paused");
    }
    @Override
    protected void onStop() {
        super.onStop();
        Log.i(TAG,"Activity Life Cycle : onStop : Activity Stoped");
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i(TAG,"Activity Life Cycle : onDestroy : Activity Destroyed");
    }

}



Now add the entry of the child activity in AndroidManifest.xml



 <activity
            android:name=".ChildActivity"
            android:label="@string/app_name" >
</activity>


Now run the application by right click and Run as Android Application.


Android Emulator
Android Sample






22 comments:

  1. Thank you! I did not do this all literally, but the suggested recipes work ok! Thank you again.

    ReplyDelete
  2. Your blog has given me that thing which I never expect to get from all over the websites. Nice post guys!

    ReplyDelete