Android Explicit Intent ExampleExplicit intent specifies the component that is which class to be invoked. We can pass the information from one activity to another using explicit intent.Here, we are going to see an example to call one activity from another and vice-versa. Android calling one activity from another activity exampleLet's see the simple example of android explicit example that calls one activity from another and vice versa.activity_main.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/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/TextView01" android:layout_marginLeft="65dp" android:layout_marginTop="38dp" android:onClick="onClick" android:text="Call second activity" /> <TextView android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/Button01" android:layout_alignParentTop="true" android:layout_marginLeft="18dp" android:layout_marginTop="27dp" android:minHeight="60dip" android:text="First Activity" android:textSize="20sp" /> </RelativeLayout> activitytwo_main.xml
File: activitytwo_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/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/TextView01" android:layout_marginLeft="65dp" android:layout_marginTop="38dp" android:onClick="onClick" android:text="Call First activity" /> <TextView android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/Button01" android:layout_alignParentTop="true" android:layout_marginLeft="18dp" android:layout_marginTop="27dp" android:minHeight="60dip" android:text="Second Activity" android:textSize="20sp" /> </RelativeLayout> ActivityOne class
File: MainActivityOne.java
package com.example.explicitintent2; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class ActivityOne extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button1=(Button)findViewById(R.id.Button01); button1.setOnClickListener(new OnClickListener(){ public void onClick(View view) { Intent i = new Intent(getApplicationContext(), ActivityTwo.class); i.putExtra("Value1", "Android By Javatpoint"); i.putExtra("Value2", "Simple Tutorial"); // Set the request code to any code you like, you can identify the // callback via this code startActivity(i); } }); } } ActivityTwo class
File: MainActivityTwo.java
package com.example.explicitintent2; 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; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class ActivityTwo extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle bundle) { super.onCreate(bundle); TextView tv=new TextView(this); tv.setText("second activity"); setContentView(R.layout.activity_two); Bundle extras = getIntent().getExtras(); String value1 = extras.getString("Value1"); String value2 = extras.getString("Value2"); Toast.makeText(getApplicationContext(),"Values are:\n First value: "+value1+ "\n Second Value: "+value2,Toast.LENGTH_LONG).show(); Button button1=(Button)findViewById(R.id.Button01); button1.setOnClickListener(new OnClickListener(){ public void onClick(View view) { Intent i = new Intent(getApplicationContext(), ActivityOne.class); startActivity(i); } }); } } Output:PREVIOUS NEXT |
Android Explicit Intent Example
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment