Passing an object from one application to another in android

After many googling, I could not find a way to pass an object from one application to another application. Although I know that we can pass an object from one activity to another activity using Parcel, but how to do it between applications? My object is

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

      

Then how to do it, how do we do to pass an object between activities

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

+3


source to share


6 answers


I am assuming you are in charge of both applications.

Which of these situations is true?

  • Both applications depend on the same data store
  • The applications are fairly independent, but the user may want to share some data from one to the other.


If the first situation is correct, the usual way would be to create an Android content provider (as suggested earlier) or a service that exposes an API using AIDL. Then both applications will depend on the Content Provider or Service.

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

If, however, the apps are peer-to-peer, then the existing intent responses are good. Either one application can launch another just by using startActivity

, or perhaps you want to give the user the ability to select an object from one to another. Take a look at this: http://developer.android.com/training/sharing/send.html

+3


source


ContentProviders can be used in one sentence

from link:

Content providers are one of the main building blocks of Android applications that provide content to applications. They encapsulate data and expose it to applications through a single ContentResolver interface.



Another suggestion could be to use SharedPreferences with getSharedPreferences with correct mode . But as you can see in the link, some of the modes are deprecated from API Level 17.

Hope this helps if not comment below

+1


source


There is another better way to use these objects and methods in the global library and add this library to your Android project. You can declare your own classes in this library and the fther function is a jar file where you can put the taht classes in the jar file and you can use it ... this link might be helpful to you ....... ....... How do I create my own Android development library to be used in every program you write?

+1


source


If you need to exchange relatively small data, use General Settings , which are 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 can also use Content Provider if you have big enough data.

0


source


Unfortunately, you cannot share an object between applications as it is if its not Parcelable.

But you can do it with services and AIDL: http://developer.android.com/guide/components/aidl.html

-1


source


Transfer of objects b / w activity:

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

      

-2


source







All Articles