Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

creating bundle and sending over to new activity

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?

like image 413
user1050632 Avatar asked Feb 16 '12 05:02

user1050632


People also ask

How do I transfer a bundle from one activity to another?

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.

How do you create a new activity?

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.

What is the difference between a bundle and an intent?

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() .

How can we send data from one Android activity to another using intent?

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.


1 Answers

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);

like image 137
Shashank Kadne Avatar answered Sep 20 '22 17:09

Shashank Kadne