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?

4

2 回答 2

2

您应该使用startActivityForResult并覆盖onActivityResult回调。这样您就可以安全地更新您的数据,而无需知道您来自哪个活动。

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();
 } 
}
于 2013-05-27T09:15:27.333 回答
1

在 A 类和 B 类的意图中,放置一个类似于布尔值“isclassA”的参数并对其进行测试以了解。在 A 类的意图:

intent.putExtra("isfromclassA",true);

B类的意图:

intent.putExtra("isfromclassA",false);

因此,在您的 C 类中:

boolean isfromclassA = intent.getBoolExtra("isfromclassA");
于 2013-05-27T09:25:52.967 回答