C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Android Explicit Intent ExampleAndroid Explicit intent specifies the component to be invoked from activity. In other words, we can call another activity in android by explicit intent. We can also 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.xmlFile: activity_main.xml
ActivityOne classFile: MainActivityOne.java
package example.TheDeveloperBlog.com.explicitintent; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; public class FirstActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_first); } public void callSecondActivity(View view){ Intent i = new Intent(getApplicationContext(), SecondActivity.class); i.putExtra("Value1", "Android By TheDeveloperBlog"); 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_main.xmlFile: activitytwo_main.xml
ActivityTwo classFile: MainActivityTwo.java
package example.TheDeveloperBlog.com.explicitintent; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Toast; public class SecondActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); 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(); } public void callFirstActivity(View view){ Intent i = new Intent(getApplicationContext(), FirstActivity.class); startActivity(i); } } Output:
Next TopicAndroid StartactivityForResult Example
|