java 片段中是否有像 setResult() 这样的方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36495842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 01:31:47  来源:igfitidea点击:

Is there a method like setResult() in fragment?

javaandroidandroid-fragments

提问by PersianBlue

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.

我正在使用一个片段。我在onResult()方法中遇到错误。我需要一个setResult(RESULT_OK, data)可以在我的片段中使用的替代方法。请帮忙。

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);
  }
}

回答by marcinj

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();

回答by G_Artem

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.

启动 Fragment 时。

Like this:

像这样:

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

And then, in YourFragment

然后,在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);

回答by Ragesh Ramesh

Use

利用

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

回答by Abhishek Patel

Use this it may be help to you..

使用这个它可能对你有帮助..

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