0

I am working with ViewPager i.e on top of the MainActivity class and the Viewpager class extends fragment.

The problem is that, normally when we need a class to return some result then while passing the intent we use startActivityforresult(intent,int) hence it passes the result captured in the secondactivity the class from where it's been called.

But as i am working with viewpager on top of the mainactivity and i am using floating action button, when i click the button to open the second activity it returns the result to the mainactivity but not the viewpager class.

So my question is how can i pass the result taken from the secondactivity to my desired class?

Update:: MainActivity.java this is my main class which is using the intent as well as receiving the result from the second activity class ActivityTwo

What i have done here is

startActivityForResult(intent,1);
public void onActivityresult(i,j,intent){MyFragment fragment;
fragment.onActivityReusult(i,j,intent);//Here i have passes the values      
received by this class to the fragment class where i need the values but it's not working
}
silverFoxA
  • 4,549
  • 7
  • 33
  • 73

2 Answers2

0

You can use a Bundle. Take a look at this topic : Bundle

bookDescFragment = new BookDescFragment();
Bundle args = new Bundle();
args.putInt(BookDescFragment.MyVariabl, VariableValue);
myFragment.setArguments(args);
Community
  • 1
  • 1
ZAIRI Oussama
  • 745
  • 9
  • 13
0

The best way to do that is to create an Interface with one method. Then the fragment implements this Interface. After that, you might call this method in the onActivityResult. You just get the desired fragment in the onActivityResult and call the method from its Interface

EDIT:

Interface example:

public interface Interface_example {

    public void onActivityResult();

}

Fragment example:

public class Fragment_example extends Fragment implements Interface_example {

    @Override
    public void onActivityResult() {

        //Here you can do whatever you want

    }

}

Activity example:

public class Activity_example extends FragmentActivity {

    private ArrayList<Fragment> mFragmentList;
    private ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_example);

        mFragmentList = new ArrayList<Fragment>();
        mFragmentList.add(new Fragment_example());

        mViewPager.setAdapter(new SimpleTabPagerAdapter(
                getSupportFragmentManager(), mFragmentList));

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        Fragment _fragment = mFragmentList.get(mViewPager.getCurrentItem());

        if(_fragment instanceof Fragment_example){

            // This line will call that method previously created
            ((Fragment_example) _fragment).onActivityResult();

        }

    }

}
Lennon Spirlandelli
  • 3,131
  • 5
  • 26
  • 51