Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 790
goober_nut Avatar asked Feb 28 '14 00:02

goober_nut


People also ask

Can you call an activity from a fragment?

Best way of calling Activity from Fragment class is that make interface in Fragment and add onItemClick() method in that interface. Now implement it to your first activity and call second activity from there.

Can we navigate from activity to fragment?

If you want to go back from Activity to Fragment. This is very simple just override onBackPressed() in your activity and call onBackPressed where you want.


2 Answers

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();
}
like image 68
Kevin Coppock Avatar answered Nov 15 '22 04:11

Kevin Coppock


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

like image 21
rperryng Avatar answered Nov 15 '22 05:11

rperryng