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?
            Asked
            
        
        
            Active
            
        
            Viewed 180 times
        
    0
            
            
         
    
    
        Avadhani Y
        
- 7,566
- 19
- 63
- 90
2 Answers
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
- 
                    Can u please help me with a sample? – Avadhani Y May 27 '13 at 09:16
- 
                    @Aʌɐpɥɐuı: Have a look at `http://stackoverflow.com/questions/10407159/android-how-to-manage-start-activity-for-result` – Mehul Joisar May 27 '13 at 09:18
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
- 
                    This is simple... I have done in the same way u suggested... Thanks – Avadhani Y May 27 '13 at 10:15