在这种情况下如何从Activity获取数据?

时间:2012-09-06 07:49:03

标签: android android-intent android-activity

Activity_A:

{
   //calling Activity B where i find the user current location lat and lng
   Intent intent = new Intent(Activity_A.this, Activity_B.class);
   startActivity(intent);
}

Activity_B
{
   //after getting user lat and lng, use them in Activity_C to show the initial marker position
   Intent intent = new Intent(Activity_B.this, Activity_C.class);
   startActivity(intent);
   //i don't this Activity_B to show up on back press in Activity_C hence finish(), anyways this Activity_B has no layout
   finish();
}

Activity_C:

{
   //Show a map with marker at position using lat, lng values from Activity_B, allow user to drag the marker and get the new lat, lng. and then finish this Activity to go to Activity_A
   finish();
}

我希望此Activity_C将新lat,lng的值发送回ACtivity_A,而不使用任何共享首选项或全局变量,我的意思是使用startActivityForResultonACtivityResult

2 个答案:

答案 0 :(得分:1)

使用setResult和内部活动A获取OnActivityResult内的所有数据。但是从A到B的启动活动需要将startActivityForResult与结果代码一起使用。否则,您可以使用broadcast receivers独立于您可以在任何活动类中发送和接收的所有活动。

答案 1 :(得分:0)

You can try chaining the activities like this::


Activity_A:

{
   //calling Activity B where i find the user current location lat and lng
   Intent intent = new Intent(Activity_A.this, Activity_B.class);
*****startActivityForResult******

   startActivity(intent);
}

Activity_B
{
   //after getting user lat and lng, use them in Activity_C to show the initial marker position
   Intent intent = new Intent(Activity_B.this, Activity_C.class);
   startActivity(intent);
   //i don't this Activity_B to show up on back press in Activity_C hence finish(), anyways this Activity_B has no layout
*****startActivityForResult******
   finish();
}

Activity_C:

{
   //Show a map with marker at position using lat, lng values from Activity_B, allow user to drag the marker and get the new lat, lng. and then finish this Activity to go to Activity_A
   finish();
}