If you are new to Android App Development and understand how an Activity works, the next thing that will come to your mind is "How to switch from one Activity to another based on an action - such as button click?"
Let's see a code example of how you can do that!
Let's say you have created a second activity with the name MySecondActivity.java, to open this activity you would need to add the blow line of code on your method where the action is performed,
Intent indent = new Intent(getApplicationContext(), MySecondActivity.class);
startActivity(indent);
How to pass data from one Activity to another?
Now that you know how to call one activity from another using Indent, let's see how we can pass data just like we can do using an HTTP GET/Post request,
Intent indent = new Intent(getApplicationContext(), MySecondActivity.class);
indent.putExtra("key","value");
indent.putExtra("key1,"value1");
startActivity(indent);
How to read the data from one Activity to another?
Now that we know how to send data from one activity to other, let's see how the other activity can read that passed data!
Intent intent = getIntent();
String data = indent.getStringExtra("key");
String data1 = indent.getStringExtra("key1");
startActivity(indent);
Related post: https://code2care.org/q/pass-data-between-two-activities-intent
Have Questions? Post them here!
- appcompat_v7 errors after updates to API level 21 Material Theme
- adb: The Android Debug Bridge and Commands
- The Android Virtual Device myEmulator is currently running an emulator and cannot be deleted.
- How to make TextView Text Transparent [Android]
- Android is starting optimizing... app 1 of 1
- How to make Text in TextView bold and italic in Android
- Android Development - How to switch between two Activities
- Android : IOException: Unable to open sync connection!
- Android Toolbar example with appcompat_v7 21
- How to Change Android Title Bar Color?
- How to Add Padding to Android TextView
- JavaScript : redirect page to other url
- Programmatically Send an Email from Android App using Intent
- What is Android Toast.LENGTH_SHORT and Toast. LENGTH_LONG durations
- [Soluiton] You already have the latest version of Android Studio installed
- Detect swipes on Android Activity
- Add Buttons at the bottom of Android Layout xml file
- Android Lint app_name is not translated in af (Afrikaans) am (Amharic) ar (Arabic) bg (Bulgarian)
- Hide Navigation Bar from Android Screen Activity
- Android Disable EditText from Auto Focus on Activity load
- Toast not getting displayed Android App
- How to Enable Developers Option in Android Phones Settings
- Android : Duplicate registration for activity com.example.abc
- Android ListView turns Black or Flickers while Scrolling
- Android Studio Ctrl Shift o auto import not working
- Fix - Error:Invalid Gradle JDK configuration found (Android Studio) - Gradle
- Split a String in Java with Examples - Java
- Android Studio : Connection Error : Failed to download patch file - Android-Studio
- Set Title to Android AlertDialog - Android
- Force Gradle to use specific Java JDK Version - Gradle
- How to do a Reverse Image Search Using Google Tools - Google
- How to find the Length of ArrayList in Java - Java
- Get MD5 Hash as Checksum of a String in Python - Python