Wednesday, 31 December 2014

Explicit Intent Example

There are two types of Intent navigation between activities
1. Explicit Intent Navigation
2. Implicit Intent Navigation

Following is an example of explicit Intent Navigation

step1: create new android application projects
step2: create new activity<SecondActivity.java>
step3: create new xml file<second_page.xml> under layout folder
step4: register the activity in manifest.xml file



activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/next_page" />

</RelativeLayout>

MainActicity.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(),
SecondActivity.class);
startActivity(i);
finish();
}
});
}
}

strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Exlicit Intent</string>
    <string name="title_activity_second">SecondActivity</string>
    <string name="second_activity">This is second Activity</string>
    <string name="next_page">Go to second Activity</string>
</resources>

second_page.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="24dp"
        android:layout_marginTop="42dp"
        android:text="@string/second_activity"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

SecondActivity.java
import android.app.Activity;
import android.os.Bundle;

public class SecondActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_page);
}
}

Note: add activity registration in manifest.xml as follows

AndroidManifest.xml
<application>
-
-
-
-
        <activity
            android:name="com.example.exlicitnavigationexample.SecondActivity"
            android:label="@string/title_activity_second" >
        </activity>
</application>

1 comment: