3

After lot of googeling, I could not find any way to pass a object from one application to other application.Though I know that we can pass object from one activty to other activity using Parcel but how to do this between applications? My object is this

public class MyObject
{
    private String name;
    public String getName()
    {
        return this.name;
    }
    public void setName(String name)
    {
        this.name=name;
    }
}

Then how to do this like we do for passing object between activities

intent.putExtra("key",new MyOject());

Atul Bhardwaj
  • 6,647
  • 5
  • 45
  • 63

6 Answers6

3

I'm assuming you're in charge of both applications.

Which of these situations is true?

  • Both applications are dependent on a single data store
  • The applications are fairly independent, but the user might want to share some data from one to the other

If the first situation is true, the normal way would be to create an Android Content Provider (as suggested previously) or a Service, which exposes an API using the AIDL language. Both applications would then depend upon the Content Provider or the Service.

See here for information about AIDL and services: http://developer.android.com/reference/android/app/Service.html http://developer.android.com/guide/components/aidl.html

If however the applications are peers, then the existing answers about Intents are good. Either one application can trigger another simply using startActivity, or perhaps you want to provide a way for a user to choose to share the object from one to the other. Look at this: http://developer.android.com/training/sharing/send.html

Adrian Taylor
  • 4,054
  • 2
  • 24
  • 26
  • http://developer.android.com/training/sharing/send.html, By using this solution ,we can share only those object(class) which are known to both the application (e.g URI parcel) but problem is to send a object which is known to one app but not in other – Atul Bhardwaj Feb 06 '13 at 07:13
  • If the Object is not known, to which object are you going to map the incoming object? – Tobrun Feb 06 '13 at 10:51
  • 1
    Could you tell us more about the exact use-case? I can't see how it's logically possible to send some data from one application to another, unless the second application also understands the data. – Adrian Taylor Feb 06 '13 at 14:00
  • @AdrianTaylor I have two application created by me .And both depends on each other. – Atul Bhardwaj Feb 08 '13 at 15:42
  • 5
    OK. So, create an Android "library project", which contains a single class - the definition of your object. Your object must implement Parcelable, and contain a static final variable called CREATOR of type Parcelable.Creator. Then, arrange for both apps to depend upon the library project - they'll be able to use the class. Finally, you can then add the Parcelable class as an extra to the intent using putExtra. Hope this does the trick! – Adrian Taylor Feb 08 '13 at 15:48
  • "Library Project" is the key part in here, without having a parcelable in library, just copying them into applications are not working. – Berkay Turancı Jun 04 '15 at 11:46
1

One suggestion could be using ContentProviders

from reference:

Content providers are one of the primary building blocks of Android applications, providing content to applications. They encapsulate data and provide it to applications through the single ContentResolver interface.

Another suggestion could be using SharedPreferences, with getSharedPreferences with a correct mode. But as you can see in the reference some modes are deprecated since API Level 17.

Hope this helps, if not comment below

Tobrun
  • 18,291
  • 10
  • 66
  • 81
1

There is another best way to use these objects and methods in a library which is global and add that library to your android project.You can declare your own classesin that library,and anther feature is jar file you can place taht classes in jar file and you can re use it.... this link may be useful to you..............How to create your own library for Android development to be used in every program you write?

Community
  • 1
  • 1
Ramz
  • 658
  • 1
  • 7
  • 19
0

If you have to share relatively very small data use Shared Preferences which is easy to implement.

public static void addData(Context context, String data) {
    SharedPreferences sharedPref = context.getSharedPreferences(app_name, 0);
    SharedPreferences.Editor editor = sharedPref.edit();
    editor.putString("Name", data);
    editor.commit();
    }

public static String getData(Context context) {
    SharedPreferences sharedPref = context.getSharedPreferences(app_name, 0);
    String fontSize = sharedPref.getString("Name", "default");
    return fontSize;
    }

Alternatively, you could also use a Content Provider if you have fairly large data.

egrunin
  • 24,650
  • 8
  • 50
  • 93
Gaurav Arora
  • 17,124
  • 5
  • 33
  • 44
-1

Unfortunately you can't share an object between applications as-is if its not Parcelable.

But you can do this with a help of services and AIDL: http://developer.android.com/guide/components/aidl.html

Vsevolod
  • 512
  • 1
  • 5
  • 16
-2

Passing object b/w activities :

public class MyObject  implements Parcelable{

    private String name;
    public String getName()
    {
    return this.name;
    }
    public void setName(String name)
    {
     this.name=name;
    }

    public MyObject() {

    }

    // Parcelling part
    public MyObject(Parcel in){
        this.name = in.readString();
    }


    @Override
    public int describeContents() {
        return 0;
    }


    @Override
    public void writeToParcel(Parcel dest, int arg1) {
        dest.writeString(this.name);
    }



    public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
        public MyObject createFromParcel(Parcel in) {
            return new MyObject(in); 
        }

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


//set Intent
Intent i= new Intent();
MyObject  o;
i.putExtra("obj", o);



//getIntent
Bundle data = getIntent().getExtras();
MyObject obj = data.getParcelable("obj");
Anand
  • 2,875
  • 1
  • 18
  • 16