I"m creating a bundle in one activity, then extracting it in another activity
here is when it's created in he main activity
//Create bundle to reference values in next class
Bundle bundle = new Bundle();
bundle.putInt("ODD", odd);
bundle.putInt("EVEN", even);
bundle.putInt("SMALL", small);
bundle.putInt("BIG", big);
//After all data has been entered and calculated, go to new page for results
Intent myIntent = new Intent();
myIntent.setClass(getBaseContext(), Results.class);
startActivity(myIntent);
//Add the bundle into myIntent for referencing variables
myIntent.putExtras(bundle);
Then when I extract on the other activity
//Extract the bundle from the intent to use variables
Bundle bundle = getIntent().getExtras();
//Extract each value from the bundle for usage
int odd = bundle.getInt("ODD");
int even = bundle.getInt("EVEN");
int big = bundle.getInt("BIG");
int small = bundle.getInt("SMALL");
The application crashes when I'm extracting the bundle on the second activity. But when I comment out the extraction of the bundle. The app runs fine. So I've narrowed it down to that.
My log cat doesn't really explain what the error is, or i just dont understand it
ideas?
Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. Step 4 − Add the following code to res/layout/activity_second.
Step 1: Firstly, click on app > res > layout > Right Click on layout. After that Select New > Activity and choose your Activity as per requirement. Here we choose Blank Activity as shown in figure below. Step 2: After that Customize the Activity in Android Studio.
An Intent is also capable of holding data in the form of name-value pairs (via putExtra() ). But while overriding the onCreate() method we pass a Bundle as the parameter, which ultimately also holds values in the form of name-value pairs and is able to store information with the help of onSaveInstanceState() .
Using Intents This example demonstrate about How to send data from one activity to another in Android using intent. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.
You are adding below code after calling startActivity(myIntent);
//Add the bundle into myIntent for referencing variables
myIntent.putExtras(bundle);
Put this just before startActivity(myIntent);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With