Saturday, 24 November 2012

Implicit Intents - Same Action and Category tags to apply two Activities

Demo:



1.ImplicitIntentsDemo1 Activity:

package com.venkool;

import android.app.Activity;

public class ImplicitIntentsDemo1Activity extends Activity {
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.first);

            Button btnSecond = (Button) findViewById(R.id.btnSecond);

            btnSecond.setOnClickListener(new OnClickListener() {

                  @Override
                  public void onClick(View v) {
                        // TODO Auto-generated method stub

                        Intent intent = new Intent();
                        intent.setAction("action2");
                        intent.addCategory("category2");
                        startActivity(intent);
                  }
            });

            Button btnThird = (Button) findViewById(R.id.btnThird);

            btnThird.setOnClickListener(new OnClickListener() {

                  @Override
                  public void onClick(View v) {
                        // TODO Auto-generated method stub

                        Intent intent = new Intent();
                        intent.setAction("action2");
                        intent.setAction("action3");
                        intent.addCategory("category2");
                        intent.addCategory("category2");
                        startActivity(intent);
                  }
            });
      }
}


2.SecondScreen Activity:


package com.venkool;

import android.app.Activity;

public class SecondScreen extends Activity {
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.second);

      }
}


3.Third Screen Activity:


package com.venkool;

import android.app.Activity;

public class ThirdScreen extends Activity {
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.third);

      }
}


4.first.xml:

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

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:gravity="center"
        android:text="Implicit Intents Demo"
        android:textSize="20dp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/btnSecond"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="30dp"
        android:gravity="center"
        android:text="Second" />

    <Button
        android:id="@+id/btnThird"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="30dp"
        android:gravity="center"
        android:text="Third" />

</LinearLayout>



5.second.xml:

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

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="70dp"
        android:gravity="center"
        android:text="Second Screen"
        android:textSize="30dp"
        android:textStyle="bold" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"
        android:gravity="center"
        android:text="Implicit Intents"
        android:textSize="20dp"
        android:textStyle="bold" />

</LinearLayout>



6.third.xml:

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

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="70dp"
        android:gravity="center"
        android:text="Third Screen"
        android:textSize="30dp"
        android:textStyle="bold" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"
        android:gravity="center"
        android:text="Implicit Intents"
        android:textSize="20dp"
        android:textStyle="bold" />

</LinearLayout>



7.Colors adding Strings.xml:

    <color name="FirstBackground">#556B2F</color>
    <color name="SecondBackground">#9932CC</color>
    <color name="ThirdBackground">#4B0082</color>



8.Add activity to manifest file:

<activity
            android:label="Second Screen"
            android:name=".SecondScreen" >
            <intent-filter >
                <action android:name="action2" />
                <action android:name="action3" />

                <category android:name="category2" />
                <category android:name="category3" />

                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:label="Third Screen"
            android:name=".ThirdScreen" >
            <intent-filter >
                <action android:name="action2" />
                <action android:name="action3" />

                <category android:name="category2" />
                <category android:name="category3" />

                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
</activity>


No comments:

Post a Comment