I have Main activity that has two tabs (fragments). in a fragment I am calling a new activity by startactivityforresult. on the new activity I put the result code when back button is pressed. the problem is the "OnActivityResult" in the fragment is never called and I can't figure the why!.
Fragment Code:
RequestListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
view.setSelected(true);
Intent SelectedRentRequest = new Intent(getActivity(), SelectedRentRequest.class);
SelectedRentRequest.putExtra("CarNumbers", SCarNumbers);
startActivityForResult(SelectedRentRequest, 1);
}
});
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == 1 && data != null) {
LoadList();
Toast.makeText(getContext(), "hi", Toast.LENGTH_LONG).show();
}
}
Second Activity Code (back pressed part):
@Override
public void onBackPressed() {
Intent intent = new Intent();
setResult(1, intent);
finish();
}
Main Activity (tabs Part):
TabLayout tabLayout = (TabLayout) findViewById(R.id.Maintab);
assert tabLayout != null;
tabLayout.addTab(tabLayout.newTab().setText("سيارات محافظتك"));
tabLayout.addTab(tabLayout.newTab().setText("طلبات السيارات"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter
(getSupportFragmentManager(), tabLayout.getTabCount(), bundle);
assert viewPager != null;
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});