Android Projects

Android Development Books

Monday

Intent tutorials Part 2 with Explicit in android

Explicit intents and data transfer between activities 

The following demonstrates how you can transfer data between two activities. We will use explicit intents in this example and create two activities. The first activity will call the second one via an explicit intent. This second activity will receive data from the first one via the class "Bundle" which can be retrieved via intent.getExtras().
The second activity can be finished either via the back button on the phone or via the button. The method finish() is performed in this case. In this method you can transfer some data back to the calling activity. This is possible because we use the method startActivityForResult(). If you start an activity via this method the method onActivity result is called on the calling activity once the called activity is finshed.
Create a new Android application "de.vogella.android.intent.explicit" with the Activity "ActivityOne". Change the layout "main.xml" to the following.

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

	<LinearLayout android:id="@+id/LinearLayout01"
	android:layout_width="wrap_content" android:layout_height="wrap_content">
	<TextView android:id="@+id/TextView01" android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:text="First Activity. Press button to call second activity"
	android:minHeight="60dip" android:textSize="20sp"></TextView>
	</LinearLayout>
	<LinearLayout android:id="@+id/LinearLayout02"
	android:layout_width="wrap_content" android:layout_height="wrap_content">
        </LinearLayout>
	<Button android:id="@+id/Button01" android:layout_width="wrap_content"
	android:layout_height="wrap_content" android:onClick="onClick"
	android:text="Calling an intent"></Button>
</LinearLayout>

		
Create the layout "second.xml".

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

	<LinearLayout android:id="@+id/LinearLayout01"
	android:layout_width="wrap_content" android:layout_height="wrap_content">
	<TextView android:id="@+id/TextView01" android:layout_width="wrap_content"
	android:layout_height="wrap_content" android:text="First value from Activity 1">
        </TextView>
	<EditText android:text="@+id/EditText01" android:id="@+id/EditText01"
	android:layout_width="wrap_content" android:layout_height="wrap_content">
        </EditText>
	</LinearLayout>
	<LinearLayout android:id="@+id/LinearLayout02"
	android:layout_width="wrap_content" android:layout_height="wrap_content">
	<TextView android:id="@+id/TextView02" android:layout_width="wrap_content"
	android:layout_height="wrap_content" android:text="Second Value from Activity one">
        </TextView>
	<EditText android:text="@+id/EditText02" android:id="@+id/EditText02"
	android:layout_width="wrap_content" android:layout_height="wrap_content">
        </EditText>
	</LinearLayout>
	<Button android:id="@+id/Button01" android:layout_width="wrap_content"
	android:layout_height="wrap_content" android:onClick="onClick"
	android:text="Finished this activity"></Button>
</LinearLayout>

		
Create a new activity "ActivityTwo" via the AndroidManifest.

			
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="de.vogella.android.intent.explicit"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".ActivityOne"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    <activity android:label="ActivityTwo" android:name="ActivityTwo"></activity>
</application>
    <uses-sdk android:minSdkVersion="9" />

</manifest> 
		
Create the following coding for your two activities. The second activity will be called from the first one, displays the transferred data and if you select the button of the back button on the phone you send some data back tot the calling application.

			
package de.vogella.android.intent.explicit;

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

public class ActivityOne extends Activity {
   private static final int REQUEST_CODE = 10;


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

  public void onClick(View view) {
	Intent i = new Intent(this, ActivityTwo.class);
	i.putExtra("Value1", "This value one for ActivityTwo ");
	i.putExtra("Value2", "This value two ActivityTwo");
	// Set the request code to any code you like, you can identify the
	// callback via this code
	startActivityForResult(i, REQUEST_CODE);
    }

  @Override
   protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
	   if (data.hasExtra("returnKey1")) {
		Toast.makeText(this, data.getExtras().getString("returnKey1"),
		    Toast.LENGTH_SHORT).show();
		     }
	    }
	}
   }
		

			
package de.vogella.android.intent.explicit;

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

public class ActivityTwo extends Activity {

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		setContentView(R.layout.second);
		Bundle extras = getIntent().getExtras();
		if (extras == null) {
			return;
		}
		String value1 = extras.getString("Value1");
		String value2 = extras.getString("Value2");
		if (value1 != null && value2 != null) {
			EditText text1 = (EditText) findViewById(R.id.EditText01);
			EditText text2 = (EditText) findViewById(R.id.EditText02);
			text1.setText(value1);
			text2.setText(value2);
		}
	}

	public void onClick(View view) {
		finish();
	}

	@Override
	public void finish() {
		Intent data = new Intent();
		data.putExtra("returnKey1", "Swinging on a star. ");
		data.putExtra("returnKey2", "You could be better then you are. ");
		setResult(RESULT_OK, data);
		super.finish();
	}
} 
 
if tou want more information about this article , click Link
 More information about Intent. Link1, Link2, Link3 
 
Share this post
  • Share to Facebook
  • Share to Twitter
  • Share to Google+
  • Share to Stumble Upon
  • Share to Evernote
  • Share to Blogger
  • Share to Email
  • Share to Yahoo Messenger
  • More...

0 comments

Thanks for your comment

:) :-) :)) =)) :( :-( :(( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ :-$ (b) (f) x-) (k) (h) (c) cheer

Related Posts Plugin for WordPress, Blogger...
© Google Android Lovers
Designed by BlogThietKe Cooperated with Duy Pham
Released under Creative Commons 3.0 CC BY-NC 3.0