Home > Android Programming > How to make an activity call itself in Android

How to make an activity call itself in Android

March 6, 2012

Here is an example of how to make an activity in Android call itself via using an Intent object.

Here is the code for the main.xml layout:

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

<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Load Instances"
android:layout_centerHorizontal="true" />

<LinearLayout android:id="@+id/textlayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_centerInParent="true" >

<TextView
android:id="@+id/textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="MainActivity has not called itself yet."
android:layout_gravity="center" />

</LinearLayout>

</RelativeLayout>

Here is the AndroidManifest.xml code:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.robogenus.SelfCallingActivity"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8" />

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="MainActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>

And here is the code that goes in the MainActivity.java class file:

package com.robogenus.SelfCallingActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {   
	
   private int cnt = 1; // A field to keep count
   private String message = ""; // to hold our message
	
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);        
      
      // Here were just getting the Intent extras that were passed
      Bundle extras = getIntent().getExtras();
      if (extras != null) {
         message = extras.getString("message");
         cnt = extras.getInt("cnt");        	
         cnt++;  // incrementing cnt for the next call      	
      }
        
      TextView textview = (TextView) findViewById(R.id.textview);
      textview.setText(message); // setting the new text for our
                                 // textview ...

      Button button = (Button) findViewById(R.id.button);
      button.setOnClickListener(new View.OnClickListener() {

	 public void onClick(View v) {
	    // the message we are going to pass to 
            // the new Activity instance, cnt will be incremented	
	    message = "This is call # " + cnt + " to MainActivity Self.";
				
	    Intent intent = new Intent("MainActivity");
	    Bundle B = new Bundle();
      
            // building the bundle and adding to intent
	    B.putString("message", message);
	    B.putInt("cnt", cnt);
	    intent.putExtras(B);
	    startActivity(intent);
	}			
     });        
   }
}

That’s all there is to it. Sweet and simple, just how we like it. Although, this could be improved upon as far as making the transition to each activity instance smoother. Any suggestions? Feel free to comment. Thanks! I Hope this example helped you.