从片段我开始使用FirstActivity,从FirstActivity开始我创建了SecondActivity.how我可以从SecondActivity获取片段中的onActivityResult。 我试图在firstActivity中添加android:noHistory =“true”。
有没有办法实现这个目标? 提前致谢
FragmentActivity
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
hotelImageMain.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(myContext, HotelsByLocation.class);
intent.putExtra(HotelActivity.EXTRA_PARAM_FOR_RESULT, true);
intent.putExtra(HotelsByLocation.EXTRA_PARAM_LOCATION_NAME, App.getInstance().getDayList().get(AddHotels.viewPager.getCurrentItem()).get(App.HASHMAP_PARA_CITY_NAME));
intent.putExtra(HotelsByLocation.EXTRA_PARAM_LOCATION_ID, App.getInstance().getDayList().get(AddHotels.viewPager.getCurrentItem()).get(App.HASHMAP_PARA_CITY_ID));
startActivityForResult(intent, MY_REQUEST_CODE_TWO);
}
});
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == MY_REQUEST_CODE_TWO) {
if (resultCode == Activity.RESULT_OK) {
Toast.makeText(myContext, "Welcome Back from HotelsByLocation", Toast.LENGTH_SHORT).show();
HashMap<String, String> map = new HashMap<String, String>();
map.put(App.HASHMAP_PARA_CITY_NAME, data.getStringExtra("cname"));
map.put(App.HASHMAP_PARA_CITY_ID, data.getStringExtra("cid"));
map.put(App.HASHMAP_PARA_HOTEL_NAME, data.getStringExtra("name"));
map.put(App.HASHMAP_PARA_HOTEL_IMAGE, data.getStringExtra("img"));
App.getInstance().getDayList().set(AddHotels.viewPager.getCurrentItem(),map);
hotel_name_main.setText(data.getStringExtra("name"));
Picasso.with(myContext)
.load(data.getStringExtra("img"))
.error(R.drawable.ic_empty)
.placeholder(R.drawable.ic_empty)
.into(hotelImageMain);
}
} else if (resultCode == Activity.RESULT_CANCELED) {
// some stuff that will happen if there's no result
Toast.makeText(myContext, "you haven't selected", Toast.LENGTH_SHORT).show();
}
}
FirstActivity
mmAdapter.setOnItemClickListener(new HATwo.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
intent.putExtra(MyActivity.EXTRA_PARAM_HOTEL_ID, mPhotosList.get(position).getId());
intent.putExtra(MyActivity.EXTRA_PARAM_HOTEL_NAME, mPhotosList.get(position).getTitle());
intent.putExtra(HotelActivity.EXTRA_PARAM_FOR_RESULT, true);
//startActivityForResult(intent, MY_REQUEST_CODE_TWO);
startActivity(intent);
SecondActivity
public void onClick(View v) {
Intent intent = new Intent();
intent.putExtra("name",nameOfTheHotel );
intent.putExtra("id", hotelId);
setResult(Activity.RESULT_OK, intent);
finish();
}
答案 0 :(得分:0)
您可能希望将startActivity从fragment用于firstactivity,然后将startActivityForResult从firstactivity用于secondactivity。当您从第二个活动中获得结果时,您可以在第一个活动的onActivityResultResult上处理它,并使用intent从第一个活动转到碎片,并将从第二个活动获得的值传递给第一个活动的碎片。确保正确处理后台堆。
答案 1 :(得分:0)
首先取消注释
startActivityForResult(intent, MY_REQUEST_CODE_TWO);
来自onItemClick
的{{1}}方法和评论
FirstActivity
在//startActivity(intent);
onActivityResult
FirstActivity
说明:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == MY_REQUEST_CODE_TWO) {
Intent intent = new Intent();
intent.putExtra("name", data.getExtras().getString("name"));
intent.putExtra("id", data.getExtras().getString("id"));
setResult(Activity.RESULT_OK, intent);
finish();
}
}
的结果将发送至SecondActivity
,FirstActivity
的结果将发送至FirstActivity
答案 2 :(得分:0)
通过调用将活动结果值从活动传递到片段->
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
for (Fragment fragment : getSupportFragmentManager().getFragments()) {
if (fragment != null) {
fragment.onActivityResult(requestCode, resultCode, data);
}
}
}
}
然后在片段中调用活动结果
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.e("onF**kingHere*frg", String.valueOf(requestCode) + " " + resultCode);
}