Handling onActivityResult in Android app having more than one activity

后端 未结 4 639
小鲜肉
小鲜肉 2020-12-10 08:05

In my android app, I have a main activity which creates two other sub activites through intent. Now, both the sub activity return result to the main activity. In my main act

4条回答
  •  青春惊慌失措
    2020-12-10 08:21

    That's what the requestCode is for. So you'd have a setup like this

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        switch(requestCode)
            case ACTIVITY1:
               if(resultCode == RESULT_OK)
                  Toast.makeText(getApplicationContext(), "Activity 1 returned OK", Toast.LENGTH_LONG).show();
               break;
            case ACTIVITY2:
               if(resultCode == RESULT_OK)
                  Toast.makeText(getApplicationContext(), "Activity 2 returned OK", Toast.LENGTH_LONG).show();
               break;
    }
    

    Where ACTIVITY1 and ACTIVITY2 are constants in your Activity. You'd call them like so:

    startActivityForResult(activity1Intent, ACTIVITY1);

    and

    startActivityForResult(activity2Intent, ACTIVITY2);

提交回复
热议问题