from fragment i started FirstActivity and from FirstActivity i started a SecondActivity.how i can get onActivityResult in fragment from SecondActivity. I tried to Add android:noHistory="true" in firstActivity.
is there a way to achieve this? Thanks in advance
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();
}
You might want to use startActivity from fragment to firstactivity and then startActivityForResult from firstactivity to secondactivity. When you get your result from secondactivity you handle it on your firstactivity's onActivityResultResult and use intent to go from firstactivity to the fragment and pass the value obtained from second activity to fragment from firstactivity. make sure to handle the backstack correctly.
First of all uncomment that
startActivityForResult(intent, MY_REQUEST_CODE_TWO);
from onItemClick
method of FirstActivity
and comment
//startActivity(intent);
Add this onActivityResult
in 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();
}
}
Explanation:
The result of SecondActivity
will be delivered to FirstActivity
and the result of FirstActivity
will be delivered to the FragmentActivity
pass on activity result value from activity to fragment by call ->
@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);
}
}
}
}
and then call on activity result in fragment
@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);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.