有没有像 setResult() 这样的片段方法?
2022-09-01 15:22:03
我正在使用片段。我在方法中收到错误。我需要一个可以在我的片段中使用的替代方法。请帮忙。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);
}
}