Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a method like setResult() in fragment?

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);
  }
}
like image 305
Khadija Daruwala Avatar asked Apr 08 '16 09:04

Khadija Daruwala


People also ask

Can I use onActivityResult in fragment?

If you use Android support library (AppCompat). This doesn't work if the fragments are nested, as only the top fragment will be in the list. I believe that super. onActivityResult() is already calling onActivityResult() on all of the fragments in the list, so this idea is redundant.

How pass data from fragment to activity Kotlin?

Step 1 − Create a new project in Android Studio, go to File ⇉ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. Step 3 − Create two FragmentActivity and add the codes which are given below.

How do I use registerForActivityResult?

registerForActivityResult() takes an ActivityResultContract and an ActivityResultCallback and returns an ActivityResultLauncher which you'll use to launch the other activity. An ActivityResultContract defines the input type needed to produce a result along with the output type of the result.


4 Answers

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();
like image 182
marcinj Avatar answered Oct 03 '22 09:10

marcinj


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);
like image 22
G_Artem Avatar answered Oct 03 '22 08:10

G_Artem


Use this it may be help to you..

getActivity().setResult(Activity.RESULT_OK, data);
like image 43
Abhishek Patel Avatar answered Oct 03 '22 10:10

Abhishek Patel


Use

getActivity().setResult(Activity.RESULT_OK, data);
like image 22
Ragesh Ramesh Avatar answered Oct 03 '22 08:10

Ragesh Ramesh