Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling onActivityResult in Android app having more than one activity

Tags:

android

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 activity, how do I handle two "onActivityResult(int requestCode, int resultCode, Intent data)" since it cant have two methods with same name in a given class. Hope my question is clear..

Thanks

like image 608
androider Avatar asked Aug 31 '11 19:08

androider


1 Answers

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;
        }
}
like image 111
Femi Avatar answered Oct 02 '22 09:10

Femi