Skip to content Skip to sidebar Skip to footer

How To Pass Object To A New Activity? (how To Inject)

I want to pass an object (not data!) that implements some interface (ex. IConnect ) to a new Activity. Activity will call some methods from this object to acquire data. How to do t

Solution 1:

MyParcelable.java

public class MyParcelable implements Parcelable { 

    private String mVal; 

    public MyParcelable(String val) { 
        mVal = val; 
    } 

    public static final Parcelable.Creator<MyParcelable> CREATOR = 
        new Parcelable.Creator<MyParcelable>() { 
        public MyParcelable createFromParcel(Parcel in) { 
            String s = in.readString(); 
            return new MyParcelable(s); 
        } 

        public MyParcelable[] newArray(int size) { 
            return new MyParcelable[size]; 
        } 
    }; 

    public String getValue() { 
        return mVal; 
    } 

    public void writeToParcel(Parcel p) { 
        p.writeString(mVal); 
    } 

    public String toString() { 
        return "MyParcelable[val=" + mVal + "]"; 
    } 

} 

SendParcelable.java

public class SendParcelable extends Activity { 
    @Override 
    public void onCreate(Bundle icicle) { 
        super.onCreate(icicle); 
        Intent intent = new Intent(this, ReceiveParcelable.class); 
        Parcelable p = new MyParcelable("test"); 
        intent.putExtra("myparcelable", p); 
        Log.i("ParcelDemo", "Sending Parcelable"); 
        startActivity(intent); 
    } 
} 

ReceiveParcelable.java

public class ReceiveParcelable extends Activity { 
    @Override 
    public void onCreate(Bundle icicle) { 
      super.onCreate(icicle); 
      Intent intent = getIntent(); 
       Parcelable p = intent.getParcelableExtra("myparcelable"); 
        Log.i("ParcelDemo", "Got Parcelable " + p); 
        if (p != null && p instanceof MyParcelable && 
            ((MyParcelable) p).getValue().equals("test")) { 
            Log.i("ParcelDemo", "Success!"); 
        } else { 
            Log.i("ParcelDemo", "Failure!"); 
        } 
    } 
} 

Hope this helps!


Solution 2:

There is an entry in Android documentation - How do I pass data between Activities/Services within a single application? If you're talking about non-persistent objects, your options will be:

  • Singleton class
  • A public static field/method
  • A HashMap of WeakReferences to Objects

By the way, I don't really understand the distinction you make between objects and data. An object contains fields and a set of methods to work with them. However, it doesn't matter if this object has some methods or not, they do not make a part of its state. If you pass an object, you only need to pass its state, so you can save the state using serialization (through Serializable or Parcelable interface) and then restore it.


Solution 3:

If you are just trying to find a way to pass objects between activities you can override the Application class.

Way to use application class.

Extend the application class and add your object as its attribute. In any activity, if you call the below code, it will return a singleton.

MyApplication appContext = (MyApplication) getApplication();

To make this work you need to add this to the application tag of the manifest file

 android:name=".MyApplication"

This method is used to pass values and objects around the app.


Solution 4:

Parcelable is an option. It will write the data from your object and create a new object from the data. Its like saving and loading data.

Take a look at http://developer.android.com/reference/android/os/Parcelable.html to see how you should implement it.


Solution 5:

For now, i did for destination Activity:

    String className = getIntent().getStringExtra("com.myproject.ModelClass");
    IModel model = (IModel)Class.forName(className).newInstance();

And i have a specific implementaction in the Activity.


Post a Comment for "How To Pass Object To A New Activity? (how To Inject)"