1

I created my Base activity in separated process by adding to it's <activity> fragment in manifest:

android:process=":login_exception_process" 

In Base activity I initiate my connection objects, and start next Activitty. But from another Activity when I try to get this instances of connection objects they are NULL.

Objects initiated by Base activity are saved in separated classes in static variables. How can I access this variable?

Cœur
  • 37,241
  • 25
  • 195
  • 267
igor.stebliy
  • 113
  • 1
  • 8
  • You can do one more thing. Create one more class which extends application. Declare all your connection variables over there and it will be available to all the activities of app. – Zohra Khan Apr 15 '14 at 14:03
  • 1
    @ZohraKhan - no, that is not really true. There will be variable available in each process, but they will be *different* ones (different or null instances), and so useless for this purpose. – Chris Stratton Apr 15 '14 at 14:09
  • You must use some means of interprocess communication - Intent extras as two answers are suggesting below, Binder IPC (commonly seen with AIDL style service binding), unix domain sockets, named pipes, even (at an extreme of setup) shared memory. – Chris Stratton Apr 15 '14 at 14:11

2 Answers2

1

Passing values to another Activity

You have to start your next activity sending an extra in an Intent.

Intent intent = new Intent(this, NextActivity.class);
intent.putextra("keyName","value");
startActivity(intent);

And your next Activity, you can retrieve this value by doing this:

String data = getIntent().getExtras().getString("keyName");

Here is the documentation for that.


Getting a result from an Activity

Starting another activity doesn't have to be one-way. You can also start another activity and receive a result back. For example, if you want to start the NextActivity and return a value back from NextActivity to BaseActivity, you can use startActivityForResult().

Here is the documentation for that.


Update

Sending objects via intent can be done with two options:

Pseudo-codes

Serializable

intent.putExtra("MyClass", your_object);  

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

Parcelable

Intent mIntent = new Intent(this, NextActivity.class);  
Bundle mBundle = new Bundle();  
mBundle.putParcelable(PAR_KEY, your_object);  
mIntent.putExtras(mBundle);
startActivity(mIntent);  

Despite Serializable seems to be the easiest option, follow this tutorial it will teach you both ways.


Hope this helps you.

rogcg
  • 10,451
  • 20
  • 91
  • 133
  • I need to send big objects, and support methods from previous process - this will work? – igor.stebliy Apr 15 '14 at 13:59
  • @igor.stebliy what kind of objects you want to send? And what you mean when you say "support methods from previous process"? – rogcg Apr 15 '14 at 14:06
  • I create instance of my internate client, methods like reauntefication, i suppouse methods will be accessible. – igor.stebliy Apr 15 '14 at 14:08
  • @igor.stebliy see the update of my answer. About the methods support, you want to access the methods from the previous **Activity** in your current **Activity**? – rogcg Apr 15 '14 at 14:12
0

You should use Extras

Basically, when creating your intent you have to put extras:

intent.putExtra("name", "My name is John");

then when you want to get it back when you start the next activity:

this.getIntent().getExtras().getString("name");

You can do this with any type if you convert the Object to a byte array.

Community
  • 1
  • 1
John
  • 3,769
  • 6
  • 30
  • 49