29

I am using a fragment. I am getting an error in the onResult() method. I need a substitute method for setResult(RESULT_OK, data) that I can use in my fragment. Please help.

CalendarFragment:

package app.pal.study.samplestudy;

import android.app.Fragment;
import android.content .Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import java.util.Date;
import java.util.List;

public class CalendarFragment  extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_calendar, container, false);
    return rootView;
}


@Override
public void onResume() {
    super.onResume();
    refresh();
}

private void refresh() {
    CalendarEventDataSource dataSource = new CalendarEventDataSource(getActivity());
    dataSource.openReadOnlyDB();
    final List<CalendarEvent> calendarEvents = dataSource.getAllEvents();
    dataSource.close();
    CalAllEventsListAdapter adapter = new CalAllEventsListAdapter(calendarEvents);
    ListView listView = (ListView) getView().findViewById(R.id.all_event_list);
    listView.setAdapter(adapter);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home) {
        end();
        return true;
    }
    return super.onOptionsItemSelected(item);
}



public void onBackPressed() {
    end();
}

private void end() {
    Intent data = new Intent();
    data.putExtra(Constants.DATE_KEY, (Date)(getArguments().get(Constants.DATE_KEY)));
    setResult(RESULT_OK, data);
  }
}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Khadija Daruwala
  • 1,185
  • 3
  • 25
  • 54
  • what was the error..? – Abhishek Patel Apr 08 '16 at 09:16
  • 1
    setResult is used to activitys called by startActivityForResult. You should use a interface callback. – Nanoc Apr 08 '16 at 09:18
  • @AbhishekPatel Error:(58, 19) error: cannot find symbol variable RESULT_OK – Khadija Daruwala Apr 08 '16 at 09:21
  • i think correct will be using callback interface and setResult(RESULT_OK, data); in your nested activity. About callback you can read here [android developers guide](http://developer.android.com/intl/ru/training/basics/fragments/communicating.html) – Alexander Apr 08 '16 at 09:21
  • @Nanoc how do i use interface callback? – Khadija Daruwala Apr 08 '16 at 09:23
  • use `Activity.RESULT_OK` instead of simple `RESULT_OK`. – Abhishek Patel Apr 08 '16 at 09:23
  • Create a interface with a method that you will implement on your activity and pass the reference to your fragment so it can call that method on your activity (this is asuming you are trying to make a fragment return a response to its activity) you can use getActivity with a cast to make this too. – Nanoc Apr 08 '16 at 09:24

4 Answers4

38

You should call it on your fragment owning activity:

 getActivity().setResult(Activity.RESULT_OK, data) 

also you might want to finish your activity:

 getActivity().finish();
marcinj
  • 48,511
  • 9
  • 79
  • 100
  • `throws an error` - what do you mean? Compiler can show error, this is during compilation. Exception can be thrown during execution - then you should look at the call stack in logcat. – marcinj Apr 08 '16 at 09:28
  • maybe also use startActivityForResult when you start your activity fragment – MojioMS Apr 08 '16 at 09:29
  • @PersianBlue I have updated to use: Activity.RESULT_OK, that was probably causing compile error – marcinj Apr 08 '16 at 09:30
  • Now use requireActivity() instead of getActivity() method to call Activity methods. – Asadullah Mumtaz Feb 01 '21 at 18:19
  • It doesn't work for me, it doesn't go inside the onActivityResult of the sender activity – Sepideh Vatankhah Jun 07 '22 at 12:00
  • @SepidehVatankhah I think you should write new question with code you are using. There were some major changes in android over last couple of years in this field. You may look into this so: https://stackoverflow.com/questions/62671106/onactivityresult-method-is-deprecated-what-is-the-alternative, or go directly to google training here https://developer.android.com/training/basics/intents/result it has a section on fragments – marcinj Jun 09 '22 at 07:48
  • @marcinj I fixed the problem with this solution https://developer.android.com/guide/fragments/communicate#fragment-result – Sepideh Vatankhah Jun 21 '22 at 13:07
16

If you starting your fragment from another fragment.

You need to use:

/**
 * Optional target for this fragment.  This may be used, for example,
 * if this fragment is being started by another, and when done wants to
 * give a result back to the first.  The target set here is retained
 * across instances via {@link FragmentManager#putFragment
 * FragmentManager.putFragment()}.
 *
 * @param fragment The fragment that is the target of this one.
 * @param requestCode Optional request code, for convenience if you
 * are going to call back with {@link #onActivityResult(int, int, Intent)}.
 */

public void setTargetFragment(Fragment fragment, int requestCode) {
}

When starting your Fragment.

Like this:

Fragment newFragment = new YourFragment();
newFragment .setTargetFragment(this, SOME_REQUEST_INT);

And then, in YourFragment

Intent data = new Intent();
data.putExtra(Constants.DATE_KEY, (Date)(getArguments().get(Constants.DATE_KEY)));
getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, intent);

Or

getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_CANCELED, null);
Sufian
  • 6,405
  • 16
  • 66
  • 120
G_Artem
  • 161
  • 6
1

Use

getActivity().setResult(Activity.RESULT_OK, data);
Ragesh Ramesh
  • 3,470
  • 2
  • 14
  • 20
1

Use this it may be help to you..

getActivity().setResult(Activity.RESULT_OK, data);
Abhishek Patel
  • 4,280
  • 1
  • 24
  • 38