1 / 15

08. Intent (Explicit)

08. Intent (Explicit). Prof. Oum Saokosal Master of Engineering in Information Systems, South Korea 855-12-252-752 oum_saokosal@yahoo.com. Agenda. What is Intent? Intent: Invoke another Activity explicitly Put Extra with Intent. What is Intent? (1). There are many uses of intent.

tasha-woods
Download Presentation

08. Intent (Explicit)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 08. Intent (Explicit) Prof. OumSaokosal Master of Engineering in Information Systems, South Korea 855-12-252-752 oum_saokosal@yahoo.com

  2. Agenda What is Intent? Intent: Invoke another Activity explicitly Put Extra with Intent

  3. What is Intent? (1) • There are many uses of intent. • You can use intents to invoke other applications from your application. • You can use intents to invoke internal or external components from your application. • You can use intents to raise events so that others can respond in a manner similar to a publish-and subscribe model.

  4. What is Intent? (2) At the simplest level, an intent is an action that you can tell Android to invoke. That is, you can invoke activity 02 from activity 01. In order to invoke like that, you need to register those activities in AndroidManifest.xml. See Example: IntentExplicit.

  5. Intent: Invoke another Activity explicitly

  6. public class Activity01 extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main01); } public void gotoProfile(View v){ Intent in = new Intent(this, Activity02.class); startActivity(in); } }

  7. main01.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Name: OumSaokosal" /> …… …… …… <Button android:onClick="gotoProfile" android:id="@+id/btnProfile" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Profile" /> </LinearLayout>

  8. public class Activity02 extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main02); } public void backtoMain(View v){ Intent in = new Intent(this, Activity01.class); startActivity(in); } }

  9. main02.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" 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:text="Relationship: Engaged" android:textAppearance="?android:attr/textAppearanceMedium" /> …… …… <Button android:onClick="backtoMain" android:id="@+id/btnBack" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Back"/> </LinearLayout>

  10. AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.kosalab" android:versionCode="1" android:versionName="1.0" > <uses-sdkandroid:minSdkVersion="10" /> <application android:icon="@drawable/ic_launcher" android:label="Intent Explicit" > <activity android:label="Activity01" android:name=".Activity01" > <intent-filter > <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:label="Activity02" android:name=".Activity02"></activity> </application> </manifest>

  11. Put Extra with Intent In PHP, you can send data from a file to another file through session. Similar to that, you can send data from one Activity to other one by putting extra with Intent. The extra data is in the form of key/value pairs.

  12. putExtra() into Intent public class Activity01 extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main01); } public void gotoProfile(View v){ Intent in = new Intent(this, Activity02.class); in.putExtra("id", "4312"); startActivity(in); } }

  13. getExtras() from getIntent public class Activity02 extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main02); Bundle extras = getIntent().getExtras(); if (extras == null) { return; } int id = 0; if(extras.getString("id") != null){ id = Integer.parseInt(extras.getString("id")); } Toast.makeText(this, "your id = " + id, Toast.LENGTH_LONG).show(); } public void backtoMain(View v){ Intent in = new Intent(this, Activity01.class); startActivity(in); } }

  14. Go on to the next slide

More Related