Home:ALL Converter>Memory spaces of two activities in separate processes

Memory spaces of two activities in separate processes

Ask Time:2016-12-19T22:39:12         Author:Kayhan Asghari

Json Formatter

Do two Activitys running in separate processes have shared memory space? Here in the Android documentation it says they have separate memory spaces.

But assume we have to Activitys: FirstActivity and SecondActivity. In the manifest, the android:process attribute is assigned to SecondActivity to make it run on a separate process. And assume FirstActivity has a public static field named A.

I start SecondActivity from FirstActivity using the startActivity() method, and I can access FirstActivity.A field and read its value which shows they shared the memory space. (but they're running in separate processes)

EDIT Here's the code:

AndroidManifest.xml

<application>

    <activity android:name=".FirstActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name=".SecondActivity"
        android:process="another_process">

    </activity>

</application>

FirstActivity

public class FirstActivity extends Activity {

    public static int A = 11111;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);

        Intent intent = new Intent(this, SecondActivity.class);
        startActivity(intent);
    }
}

SecondActivity

public class SecondActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gateway);

        Log.i("SecondActivity", "A = " + FirstActivity.A); // here
    }
}

What's happening? Is having separate memory spaces only for when two Activitys are in running from separate apps? Why the A variable is accessible from SecondActivity?

Author:Kayhan Asghari,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/41224972/memory-spaces-of-two-activities-in-separate-processes
yy