SteMMo
SteMMo

Reputation: 408

Passing data to activity

i'm developing an app that, when i press a button, downloads a XML file, put the xml data in a custom object and passes it to a second activity.
The problem is that something is wrong: when a call the startActivity() function the app crashes with a Runtime error. My code is:

public void onClickBtn1(View view)
{
    final ProgressDialog dlg = ProgressDialog.show( this, "Data wait", "Waiting data from the site ..");

    // Thread to wait data
    Thread th = new Thread() {

        public void run() {

            // Download and parse xml data
            final DatiSport dati = new DatiSport();
            boolean ret = dati.download();
            dlg.dismiss();

            // check result
            if (ret==true)
            {
                // -- Ok
                handlerUI.post( new Runnable() {

                    public void run() {
                        Intent intSec = new Intent(AICSActivity.this, SportActivity.class);
                        intSec.putExtra("datiSport", dati);
                        startActivity(intSec);
                    }
                });
            }
            else
            {

The app crashes on the startActivity() call. When i break on the startActivity() line i'm not able to look the variable called 'dati' and i guess this is not well defined.
If i substitute dati with 12345, there is not problem.

Which is the problem with dati ?


--- Changed here cause I'm not enabled to reply myself ---

Ok guys. Thanks for replies!
My guess is that i need to re-design the app data.

My first attempt was: download the XML text and accommodate the data into a (rather) complex object. This object contain a list of championships, each of them contains a list of categories, each of them contains a list of teams.

The problem is that, since the Serializable is not working, the implementation of Parcelable is too complex and it should generate almost the same data as the xml file.
I'm wondering if it should be easier passing directly the xml text to other activities (they have to show in turn the list of championships, then the categories of a selected championship, then the list of teams for a selected category...)

Any other idea?

Upvotes: 1

Views: 281

Answers (5)

SteMMo
SteMMo

Reputation: 408

I found the problem !!!
An internal class were not implementing Serializable!
In the dump window i saw the internal object 'ioe' that said that there was a NotSerializable error and the name of the class!!
Now i checked each internal class and the data is passed to the next activity.

Thanks a lot

Upvotes: 0

rajpara
rajpara

Reputation: 5203

Extract from this Answer :

Serializable is a standard Java interface. You simply mark a class Serializable by implenting the interface, and Java will automatically serialize it in certain situations.

Parcelable is an Android specific interface where you implement the serialization yourself. It was created to be far more efficient that Serializable, and to get around some problems with the default Java serialization scheme.

Extract from this answer :

Seeing Parcelable might have triggered the question, why is Android not using the built-in Java serialization mechanism? It turns out that the Android team came to the conclusion that the serialization in Java is far too slow to satisfy Android’s interprocess-communication requirements. So the team built the Parcelable solution. The Parcelable approach requires that you explicitly serialize the members of your class, but in the end, you get a much faster serialization of your objects.

After seeing some answer on StackOverFlow, i come to conclusion that Parcelable is optimized than Serialization in android.

How to make class to Parcelable ?? (Check out this, this & this tutorials)

Upvotes: 1

Swayam
Swayam

Reputation: 16354

Make your class implement Serializable interface and then pass object instances in intent extra.

To pass data from one Activity to another :

intent.putExtra("ClassName", obj); 

To retrieve data in the Second Activity from the First Activity :

getIntent().getSerializableExtra("ClassName");

Upvotes: 0

Sourabh86
Sourabh86

Reputation: 744

The Intent class has a method as

putExtra(String name, int value)

thats why it works when you put 12345 at the place of "value", but there is no overloaded version of putExtra that takes "DatiSport" object. You must ensure that "DatiSport" is Serializable or Parcelable.

See below for more info-

http://developer.android.com/reference/android/content/Intent.html#putExtra%28java.lang.String,%20java.io.Serializable%29

How to send an object from one Android Activity to another using Intents?

How to pass an object from one activity to another on Android

Upvotes: 0

programmer33
programmer33

Reputation: 652

Use a Serializable or Parcelable when passing objects

You need a class to implement the Serializable class

//to pass :
intent.putExtra("MyClass", obj);  

// to retrieve object in second Activity
getIntent().getSerializableExtra("MyClass");

Your class would look something like this;

import java.io.Serializable;

@SuppressWarnings("serial") //with this annotation we are going to hide compiler warning
public class MyClass implements Serializable {

public Deneme(Object obj){
    this.obj= obj;
}
private Object obj;

}

Upvotes: 0

Related Questions