-1

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) {

        }
    });

3 Answers3

1

In order to make onActivityResult work in your fragment you must implement it in your activity also because the setResult method returns the result to the Activity which hosts the fragment not directly to the fragment

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
    }
SaravInfern
  • 3,338
  • 1
  • 20
  • 44
1

Make sure you're using an up-to-date support library:

compile 'com.android.support:support-v7:24.1.1'

A related bug has been fixed in 23.2.


Also make sure that the parent activity has a valid launchMode specified in the manifest. E.g. having singleTask won't work and the method will be called immediately after startActivityForResult

Simas
  • 43,548
  • 10
  • 88
  • 116
0

Try adding onActivityResult in the Activity, this one will be call and dispatch to the fragment when calling super :

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
    }
Chol
  • 2,097
  • 2
  • 16
  • 26