Passing messages from multiple activities to main activity
It's quite hard to get my problem across but here goes.
I have one activity Activity 1 which from this I can open one of three activities Activity 2, Activity 3 and Activity 4, of which I want a result to come from Activity 3. Within each of these activities I can create a new intent to open any of the other activities, in turn calling finish()
on the current activity.
I have the code working for when I open Activity 3 from Activity 1 and then press the button to finish Activity 3 it sends the message perfectly. My problem lies in say opening Activity 2 from Activity 1 then from Activity 2 opening Activity 3 (in turn calling finish()
on Activity 2) and then pressing the button in Activity 3 and sending the same message to Activity 1. At the moment the result code sent through is 0, when I am trying to get it开发者_开发知识库 to send the result code '726'.
In summary I want to be able to send messages from an Activity created from an intent from an unknown level of other Activities, but the message always goes back to the same lowest level Activity if that makes sense.
I am sorry if this is hard to understand and I hope somebody can help me
I hate answering my own questions but I figured it out anyway for anybody else looking for the answer:
From the Main Activity
public void onClick2Activity(View v){
Intent i = new Intent();
i.setClass(1Activity.this, 2Activity.class);
startActivityForResult(i, 0);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==726){
Bundle bundle = data.getExtras();
int string = bundle.getInt("test");
Log.v("Extra", "" + string);
Toast.makeText(this, "Pass", Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(this, "Fail", Toast.LENGTH_LONG).show();
}
}
From 2Activity:
public void onClick3Activity(View v){
Intent i = new Intent();
i.setClass(2Activity.this, 3Activity.class);
startActivityForResult(i, 1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==726){
Intent in = new Intent();
in.putExtra("test", "test");
setResult(726,in);
finish();
}
else{
Log.v("Failed", "Failed");
}
}
From 3Activity:
public void onItemClick(){
Intent in = new Intent();
weaponID = position + 1;
in.putExtra("test", "test");
setResult(726,in);
finish();
}
Try Observer.
Also Singleton will do the work.
Or startActivityForResult on many levels (Activities).
精彩评论