Reputation: 8310
Suppose we have two simple applications, so we have two different packages.
Each of these packages has an Activity
that can be launched by clicking the application icon. Suppose that the two activities are as follows:
MyFirstActivity
, which is into the package org.firstexample.firstactivity
MySecondActivity
, which is into the package org.secondexample.secondactivity
Suppose we have launched the MyFirstActivity
activity, so it is running.
Could the MySecondActivity
activity send data directly to the MyFirstActivity
activity?
I would like the two activities (which are in different packages) can communicate with each other by exchanging data.
Upvotes: 1
Views: 1003
Reputation: 22536
If you want to pass data without resuming on destroying activity then you have to make listener for that..
public class MyFirstActivity extends Activity implements OnDataChanged {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public void onChange(int a) {
Log.e("", "a : " + a);
}
}
MySecondActivity.java
public class MySecondActivity extends Activity {
private OnDataChanged mOnDataChanged;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sendData(10);
}
public interface OnDataChanged {
public void onChange(int a);
}
private void sendData(int a) {
mOnDataChanged.onChange(a);
}
}
MySecondActivity is sending 10 to MyFirstActivity by implementing listener of MySecondActivity...
Upvotes: 0
Reputation: 132972
if you are launching MySecondActivity from MyFirstActivity then use this way:
in Activity MyFirstActivity:
Intent intent25 = new Intent(Intent.ACTION_MAIN).addCategory(
Intent.CATEGORY_LAUNCHER).setClassName("org.secondexample",
"org.secondexample.MySecondActivity").addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.addFlags(Intent.FLAG_FROM_BACKGROUND).setComponent(new ComponentName("rg.secondexample",
"org.secondexample.MySecondActivity"));
Bundle bundle = new Bundle();
bundle.putString("Name", "test");
intent25.putExtras(bundle);
getApplicationContext().startActivity(intent25);
and in MySecondActivity oncreate()
Bundle bundle = this.getIntent().getExtras();
String name = bundle.getString("Name");
Upvotes: 1