Handling onActivityResult in Android app having more than one activity

后端 未结 4 638
小鲜肉
小鲜肉 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:15

    You change the requestCode that you use when you call startActivityForResult.

    EDIT: for example, I use this:

    startActivityForResult(i, App.REQUEST_ENABLE_BT);
    

    and this:

    startActivityForResult(i, App.MANUAL_INPUT);
    

    and then you filter the results like this:

    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
            super.onActivityResult(requestCode, resultCode, data);
    
            if(resultCode == RESULT_OK){
                switch(requestCode){
                case App.REQUEST_ENABLE_BT:
                    if(resultCode != RESULT_OK){
                        Toast.makeText(this, getString(R.string.label_bluetooth_disabled), Toast.LENGTH_LONG).show();
                    }
                    break;
                case App.MANUAL_INPUT:
                    break;
            }
    }
    

提交回复
热议问题