Today, I encounter an werid problem about shared data inter-processes. I declare the MainActivity to run in another process, and write the shared data in TestApplication to be 1, and then start the SubActivity to show the shared data. Unfortunately that, the value shown in SubActivity is still 0, as a result, we have the conclusion that there are two TestApplication instance populate in two process, and the read and write of shared data is independent to each other. actually, the shared data does not been shared inter-processes any more. My question is that what is the other difference between an activity start in new process and the orignal one, such as about memory? Here are my codes:
<application
android:name=".TestApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:process="com.rlk.miaoxinli.hellokitty"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SubActivity">
</activity>
</application>
public class TestApplication extends Application {
public int mValue = 0;
}
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView mTextView;
private TestApplication mApplication;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mApplication = (TestApplication) getApplication();
setContentView(R.layout.activity_main);
mTextView = (TextView)findViewById(R.id.first);
mApplication.mValue = 1;
mTextView.setClickable(true);
mTextView.setOnClickListener(this);
}
@Override
protected void onResume() {
super.onResume();
mTextView.setText("" + mApplication.mValue);
}
@Override
public void onClick(View view) {
Intent intent = new Intent(this, SubActivity.class);
startActivity(intent);
}
}
public class SubActivity extends AppCompatActivity {
private TextView mTextView;
private TestApplication mApplication;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sub);
mTextView = (TextView) findViewById(R.id.second);;
}
}