0

I have a problem as explained here. I have a Class A, ClassB. Both have Intents to navigate to Class C. Now, I want to know from Which class i have navigated to Class C so that i will navigate to previous screen either A or B. Calling finish() in Class C doesnt help me because i need to update the data in Class A and Class B differently. Can anybody please help me. How to use Intent here? Can i know from which class i have to navigated to Class C?

Avadhani Y
  • 7,566
  • 19
  • 63
  • 90

2 Answers2

2

you should use startActivityForResult and override the onActivityResult callback. This way you can safetly update your data without know from wich activity you came from.

public class A extends Activity {
private final static int REQUEST_CODE= 1000;
public void onCreate(BUndle b) {
 ...
 Intent intent = new Intent(this, C.class);
 startActivityForResult(intent, REQUEST_CODE);
}

@override
void onActivityResult(int requestCode, int resultCode, Intent data) {
        // here through data you will receive new data
}

}

public class C extends Activity {
     public void onCreate(BUndle b) {
  // modify same data
    Intent intent = new Intent(); 

    // pute data inside intent
    setResult(Activity.RESULT_OK, intent);
    finish();
 } 
}
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
1

in your intents in class A and B, put a parameter like a boolean "isclassA" and make tests on it to know. in intent of classe A :

intent.putExtra("isfromclassA",true);

in intent of class B :

intent.putExtra("isfromclassA",false);

therefor in your class C :

boolean isfromclassA = intent.getBoolExtra("isfromclassA");
wazaminator
  • 245
  • 3
  • 15