1

I have implemented a swappable tab view by using this tutorial.

I want to pass an integer value from Fragment to FragmentActivity. How to do that?

My fragment.java:

public PastaFragment(FragmentManager fm, int hot_number) {
        super();

        this.hot_number = hot_number;
    }

Inside fragment onCreate:

mCartList = ShoppingCartHelper.getCartList();

        // Make sure to clear the selections
        for (int i = 0; i < mCartList.size(); i++) {
            mCartList.get(i).selected = false;

            hot_number = mCartList.size();
            System.out.println("hot_number11 = " + hot_number);

        }

        Fragment Pasfragment = new PastaFragment(getFragmentManager(), hot_number);
        Bundle bundle = new Bundle();
        bundle.putLong("hot_number", hot_number);

        Pasfragment.setArguments(bundle);

Inside activityfragment.java:

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

        hot_number = getIntent().getIntExtra("hot_number", hot_number);

Inside TabsPagerAdapter.java:

@Override
    public Fragment getItem(int index) {

        switch (index) {
        case 0:

            FragmentManager getFragmentManager = null;
            return new PastaFragment(getFragmentManager, index);
        }

        return null;
    }
peterh
  • 11,875
  • 18
  • 85
  • 108
modabeckham
  • 227
  • 5
  • 19
  • make a interface in fragment from where you want to pass the value and implement that interface in you're activity where you want to receive that value – Pankaj Nimgade Mar 25 '15 at 12:12
  • you can do it with callback methods – Manu Zi Mar 25 '15 at 12:12
  • IS there any code sample for me to get clear understating please – modabeckham Mar 25 '15 at 12:17
  • possible duplicate of [Passing data between a fragment and its container activity](http://stackoverflow.com/questions/9343241/passing-data-between-a-fragment-and-its-container-activity) – hrskrs Mar 25 '15 at 12:17
  • try the shared preferences.. – Ranjithkumar Mar 25 '15 at 12:47
  • [Check This](http://stackoverflow.com/questions/27484245/pass-data-between-two-fragments-without-using-activity/27484940#27484940) – Pankaj Arora Mar 25 '15 at 12:51
  • Ok. So whats the problem? have you tried anything so far? – Piyush Mar 25 '15 at 12:54
  • I have done it using interface, now I got new problem. I have the code to receive the value inside activity onCreate and I have put a debug point. How can I trigger the debug point? Because it doesn't tigger it – modabeckham Mar 25 '15 at 12:58
  • Have you seen debug option there? – Piyush Mar 25 '15 at 12:59
  • yes it triggers when I debug the application for the 1st time. But I want to trigger that point after I'm getting the value inside the fragment. – modabeckham Mar 25 '15 at 13:01
  • You need to press `F5` to trigger that line. – Piyush Mar 25 '15 at 13:01
  • It doesn't. fragment is created on top of this activityfragment. SO once the activityfragment is created then it doesn't go inside onCreate. ANything can happen like that? – modabeckham Mar 25 '15 at 13:12
  • I have updated my code, now I figure I can debug inside onResume(). Still I don't get the value. SO can U check whats the mistake with the way I'm passing it. – modabeckham Mar 25 '15 at 13:43

1 Answers1

0

Try this way:

Intent intent = new Intent(getActivity(), YourFragmentActivity.class);
intent.putExtra("YourInteger", 1);
getActivity().startActivity(intent);
Prashant Patel
  • 1,087
  • 11
  • 18