java,android:onActivityResult在一个活动中具有不同的参数

时间:2017-01-05 15:39:25

标签: android android-activity android-calendar

我有一个活动,我使用两个onActvityResults

CalendarView的一个:

@Override
    protected void onActivityResult(int requestCode, int responseCode, Intent intent) 

用于检索Google日历中的事件:

    @Override
   protected void onActivityResult(int requestCode, int resultCode, Intent data)

我如何在一项活动中拥有这两项?谢谢!

4 个答案:

答案 0 :(得分:3)

看来你还没有正确理解这个概念!这可能有助于您理解 onActivityResult

使用startActivityForResult(Intent intent, int requestCode),您可以开始另一个活动,然后在onActivityResult()方法中从该活动接收结果。{{1来自你开始另一个Activity的地方。

onActivityResult()检查这里的参数。请求代码可以从您获得结果的位置进行过滤。所以你可以使用他们的requestCodes识别不同的数据!

示例

onActivityResult(int requestCode, int resultCode, Intent data)

public class MainActivity extends Activity { // Use a unique request code for each use case private static final int REQUEST_CODE_EXAMPLE = 0x9988; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Create an Intent to start AnotherActivity final Intent intent = new Intent(this, AnotherActivity.class); // Start AnotherActivity with the request code startActivityForResult(intent, REQUEST_CODE_EXAMPLE); } //-------- When a result is returned from another Activity onActivityResult is called.--------- // @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); // First we need to check if the requestCode matches the one we used. if(requestCode == REQUEST_CODE_EXAMPLE) { // The resultCode is set by the AnotherActivity // By convention RESULT_OK means that what ever // AnotherActivity did was successful if(resultCode == Activity.RESULT_OK) { // Get the result from the returned Intent final String result = data.getStringExtra(AnotherActivity.EXTRA_DATA); // Use the data - in this case, display it in a Toast. Toast.makeText(this, "Result: " + result, Toast.LENGTH_LONG).show(); } else { // AnotherActivity was not successful. No data to retrieve. } } } } < - 这是我们用来向AnotherActivity发送数据的那个

MainActivity

注意:现在检查public class AnotherActivity extends Activity { // Constant used to identify data sent between Activities. public static final String EXTRA_DATA = "EXTRA_DATA"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_another); final View button = findViewById(R.id.button); // When this button is clicked we want to return a result button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // Create a new Intent as container for the result final Intent data = new Intent(); // Add the required data to be returned to the MainActivity data.putExtra(EXTRA_DATA, "Some interesting data!"); // Set the resultCode to Activity.RESULT_OK to // indicate a success and attach the Intent // which contains our result data setResult(Activity.RESULT_OK, data); // With finish() we close the AnotherActivity to // return to MainActivity finish(); } }); } @Override public void onBackPressed() { // When the user hits the back button set the resultCode // to Activity.RESULT_CANCELED to indicate a failure setResult(Activity.RESULT_CANCELED); super.onBackPressed(); } } MainActivity指定了startActivityForResult。我们假设你想要调用三个不同的活动来获得结果..所以有三个REQUEST_CODE个调用有三个不同的 REQUEST_CODE 。 REQUEST_CODE只是您在活动中指定的唯一键,用于唯一标识您的startActivityForResult来电。

从这些活动中收到数据后,您可以查看REQUEST_CODE是什么,那么您知道啊,这个结果来自此活动。

就像你用彩色的封面给你的情人发送邮件,并要求他们用相同的封面回复。然后,如果你收到他们的回信,你知道是谁送给你的。 awww;)

答案 1 :(得分:0)

你没有。您只有一个,但它们应该有不同的代码,如果结果来自日历视图​​或Google日历或其他内容,则可以区分这些代码。

答案 2 :(得分:0)

您可以使用requestCode方法的onActivityResult参数。使用不同的请求代码为场景调用startActivityForResult方法。

E.g。

private static int CALENDAR_VIEW_CALL = 1;
private static int CALENDAR_EVENTS_CALL = 2;

...

startActivityForResult(intent1, CALENDAR_VIEW_CALL);

...

startActivityForResult(intent1, CALENDAR_EVENTS_CALL);

现在在onActivityResult

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CALENDAR_VIEW_CALL) {
        //do the callback work for calendar view
    }
    else if (requestCode == CALENDAR_EVENTS_CALL) {
        //do the callback work after retrieving the events
    }
}

答案 3 :(得分:0)

但是你没有使用不同参数的方法,你有相同的方法,参数相同,只有参数的名称不同,你不能在java中这样做,因为语言没有真的知道哪种方法是哪种。

您应该按照其他答案检查requestCode