Question

I am able to login to skydrive using skydrive API in android. Now i want to use that same session object and liveconnectclient object in another avitivity. How can i do that?

public void onAuthComplete(LiveStatus status, LiveConnectSession session,Object userState)
    {
        if (status == LiveStatus.CONNECTED)
        {

            client = new LiveConnectClient(session);




            Log.i("message", client.toString());
            // StoreSkydriveSession(session);
            txtskydrive.setText("Connected");
            skydrivefolder.setEnabled(true);
            isSkydriveConnected=true;
        } 
        else
        {
            txtskydrive.setText("Not Connected");
            Toast.makeText(getApplicationContext(), "Not Signed In",
                    Toast.LENGTH_LONG).show();
            // this.resultTextView.setText("Not signed in.");
            client = null;
        }
    }

I am able to login .Now how can and where should i store the client and session object so that i can use it in another activites .

I have tried using beans to set and get values but when i try to get it, it throws null pointer exception.

I tried storing it in shared preferences but i cannot cast it from string to LiveConnectClient object.

Thanks :)

EDIT:

I think we can use GSON... Gson (also known as Google Gson) is an open source Java library to serialize and deserialize Java objects to (and from) JSON

SharedPreferences mPrefs = getPreferences(MODE_PRIVATE);

For save

 Editor prefsEditor = mPrefs.edit();
 Gson gson = new Gson();
 String json = gson.toJson(client); //LiveConnectClient Object
 prefsEditor.putString("MyObject", json);
 prefsEditor.commit();
For get

Gson gson = new Gson();
String json = mPrefs.getString("MyObject", "");
LiveConnectClient obj = gson.fromJson(json, MyObject.class);

I think this should work....

Was it helpful?

Solution

If you need access to same OBJECTS in different activities then easiest way to do this - just create some kind of handler class:

class LiveConnectClientHandler{

public static LiveConnectClient client;

}

Then when you got it:

public void onAuthComplete(LiveStatus status, LiveConnectSession session,Object userState) {
if (status == LiveStatus.CONNECTED) {
LiveConnectClientHandler.client = new LiveConnectClient(session);

So in other activity you may get this by - LiveConnectClientHandler.client.

But I am not sure this is right way.

First of all sending data from one activity to another should be put in intent.

In second may be you have other way to get LiveConnectSession object? Because it can be only shell for some simple string resources like access_token.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top