0

I have fragmentActivity class which implements OnTabChangeListener and acts as a host for other fragment classes. Every fragment classes is defined in different XML layout files for their own Layout.

I want to get the View from that fragment class inside the fragmentActivity class.

I have tried :

View view = myFragment.getView();
Button myButton = (Button) view.findViewById(R.id.my_button);
myButton.setOnClickListener(new MyClickListener());

But doesn't seem to work.

So does anyone know?

Karate_Dog
  • 1,265
  • 5
  • 20
  • 37
  • "I want to get the View from that fragment class inside the fragmentActivity class" why? you want to pass something back to the activity from the fragment? – Raghunandan Oct 04 '13 at 07:59
  • yes I do, because I need to get the data from the fragment class and after that I want to create a new tab after validating the data. – Karate_Dog Oct 04 '13 at 08:02
  • 2
    use interface to communicate with the activity. http://developer.android.com/guide/components/fragments.html. Check the topic **Communicating with the Activity** – Raghunandan Oct 04 '13 at 08:27

2 Answers2

2

If you want you can use the BroadcastReciver to send event notification to your activity from your fragment.

To register a broadcast reciver in your FragmentActivity do something like this :

public class MyActivity extends FragmentActivity {

    private BroadcastReceiver myBroadcastReceiver =
        new BroadcastReceiver() {
            @Override
            public void onReceive(...) {
                //YOU WILL RECEIVE YOUR BROADCAST HERE. WRITE YOUR CODE HERE TO ADD NEW TAB
            }
       });

    ...

    public void onResume() {
        super.onResume();
        ....
        registerReceiver(myBroadcastReceiver, intentFilter);
    }

    public void onPause() {
        super.onPause();
        ...
        unregisterReceiver(myBroadcastReceiver);
    }
    ...
}

Now to send Broadcast from your fragment do like this :

Intent intent=new Intent();
intent.setAction("ANY_UNIQUE_NAME");
intent.putExtra("data",EXTRA_DATA_IF_YOU_WANT);
sendBroadcast(intent);

You will receive this broadcast on your activity's on Receive Event. Do whatever you want to do there.

Vipul Purohit
  • 9,807
  • 6
  • 53
  • 76
-1

If you want to acces your component and set some data, I suggest you to make a method inside the fragment like this:

 import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;

    public class DetailFragment extends Fragment {

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.details, container, false);
            return view;
        }


        public void setSettings(){
            Button button = (Button) getView().findViewById(R.id.my_button);
            button.setOnClickListener(new MyClickListener());
        }

    }
Srinoid
  • 11
  • 1
  • 5