goober_nut
goober_nut

Reputation: 150

Calling Activity from Fragment then return to Fragment

I have an app that has a few tabs. These tabs are all fragments. On the first tab fragment, I have a text view and a button, which I press on to call an activity.

This activity displays a list of items, car names.

I want to be able to click on a car in the list and return back to the calling fragment and update the text view with the car name I selected.

Can anyone help me out with this?

Upvotes: 5

Views: 7718

Answers (2)

Kevin Coppock
Kevin Coppock

Reputation: 134664

startActivityForResult() is probably what you're looking for. So a quick example (making super-basic assumptions about your data structure -- substitute as required) would be to make your fragment override onActivityResult(), define a request code, and then start the activity using that request code:

// Arbitrary value
private static final int REQUEST_CODE_GET_CAR = 1;

private void startCarActivity() {
    Intent i = new Intent(getActivity(), CarActivity.class);
    startActivityForResult(i, REQUEST_CODE_GET_CAR);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    // If the activity result was received from the "Get Car" request
    if (REQUEST_CODE_GET_CAR == requestCode) {
        // If the activity confirmed a selection
        if (Activity.RESULT_OK == resultCode) {
            // Grab whatever data identifies that car that was sent in
            // setResult(int, Intent)
            final int carId = data.getIntExtra(CarActivity.EXTRA_CAR_ID, -1);
        } else {
            // You can handle a case where no selection was made if you want
        }
    } else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}

Then, in the CarActivity, wherever you set a click listener for your list, set the result and pass back whatever data you need in an Intent:

public static final String EXTRA_CAR_ID = "com.my.application.CarActivity.EXTRA_CAR_ID";

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    // Assuming you have an adapter that returns a Car object
    Car car = (Car) parent.getItemAtPosition(position);
    Intent i = new Intent();

    // Throw in some identifier
    i.putExtra(EXTRA_CAR_ID, car.getId());

    // Set the result with this data, and finish the activity
    setResult(RESULT_OK, i);
    finish();
}

Upvotes: 10

rperryng
rperryng

Reputation: 3253

call startActivityForResult(theIntent, 1);

In the activity started, once the user selects a car, make sure to put the car in an intent and set the result of the activity to that intent

Intent returnIntent = new Intent();
returnIntent.putExtra("result", theCar);
setResult(RESULT_OK, returnIntent);     
finish();

Then, in your fragment, implement onActivityResult

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

  if (requestCode == 1) {

     if(resultCode == RESULT_OK){      
         String result = data.getStringExtra("result");          
     }
     if (resultCode == RESULT_CANCELED) {    
         //Write your code if there's no result
     }
  }
}  //onActivityResult

Make Sure to override onActivityResult() in the fragment's hosting activity too, and call the super

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
}

This is because the parent activity hijacks the onActivityResult method, and if you don't call super() then it wont get passed to the fragment to handle it

Upvotes: 9

Related Questions