0

I'm just learning about fragments. I followed an answer of creating an interface from a fragment to pass data back to my activity based on a button click and it works perfectly.

I can't figure out though how to have my fragment listen for a specific change in my activity after everything has been created, just like how my activity does it.

Basically I have a button in my activity and another button in my fragment. When I click on the button in my activity, I'd like to send some data over so that my fragment knows to change the text of my fragment button.

Here's my current fragment

public void onClickBtn1(View v) {
    passData("abc");
}   

public interface OnDataPass {
    void onDataPass(String data);
}

OnDataPass dataPasser;

@Override
public void onAttach(Context a) {
    super.onAttach(a);
    dataPasser = (OnDataPass) a;
}

public void passData(String data) {
    dataPasser.onDataPass(data);
}

and then this in my activity

@Override
public void onDataPass(String data) {
    //do something with data
}

I'd like to somehow get this exact same thing but flip flopped. Any suggestions? Thanks!

Pam
  • 73
  • 9
  • It's even easier the other way around. Just keep a reference to the fragment when you're creating a fragment transaction, and say something like `myFragment.passData("whatever")` and make a regular method inside the fragment. – Vucko Jul 11 '17 at 07:44
  • @Pam typo in line 2 of your code. Should be: passData("abc"); – Paul Jul 11 '17 at 07:54

3 Answers3

2

you should do this

public class MainFragment extends Fragment implements MainActivity.OnDataPassedListener {

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

    @Override
    public void onDataPassed(String text) {
        Log.d("text",text);
    }
}

and in your activity

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MainFragment mainFragment = new MainFragment();
        mainFragment.onDataPassed("xx");
    }

    public interface OnDataPassedListener {
        void onDataPassed(String text);
    }
}
Amir_P
  • 8,322
  • 5
  • 43
  • 92
  • doesn't work. I've tried flip flopping them similar to what you did. But when I go to implement MainActivity.OnDataPassedListener I get "cyclic inheritance involving..." onDataPassedListener also gives me an unknown class – Pam Jul 11 '17 at 08:33
  • check my answer again @Pam – Amir_P Jul 11 '17 at 08:50
0

Basically you can do this in two ways using Bundle or Using Interface to the best of my knowledge using interface is better way of passing data from activity to fragment follow this question its already answered

akshay_shahane
  • 4,423
  • 2
  • 17
  • 30
  • Yea, I keep reading that one over and over again. Problem is that they send new data to onCreate. My fragment will have already been created and just waiting to change – Pam Jul 11 '17 at 08:37
0

Try this by getting same instance of activity in fragment by id:-

Button actBtn = (Button) getActivity().findViewById(R.id.activityButton);

Then setText() or getText() etc. same operation you do.

Updated: And if you want access (functions,or data(arraylist)) to MainActivity(Any Activity) from Fragment, you have to use instance of activity:

MainActivity mainActivity = (MainActivity) getActivity();

Then call like mainActivity.performOperation() to any method.

Nitin Patel
  • 1,605
  • 13
  • 31
  • That's not a bad idea, actually. getActivity() is used in the fragment, is there a way I can call a button that's in my fragment from my MainActivity? Thanks! – Pam Jul 11 '17 at 08:55
  • @Pam Now you want to pass value from fragment to activity, right? – Nitin Patel Jul 11 '17 at 08:56
  • @Pam There are two ways one is interface and second is like above I have given. – Nitin Patel Jul 11 '17 at 09:01
  • No, the other way around. I've figured out how to pass value from fragment to activity, but I need to now pass value from activity to an already created frament – Pam Jul 11 '17 at 09:02
  • Thanks for your help, but I'd like to do the opposite. I'd like to access a button in my fragment (to change the text, for example) from my Main Activity. Is that possible? – Pam Jul 11 '17 at 09:14
  • @Pam Ok. let me check. – Nitin Patel Jul 11 '17 at 09:16
  • @Pam For that you can try this solution:https://stackoverflow.com/a/16295170/7806873 – Nitin Patel Jul 11 '17 at 09:17
  • @Pam Or you can make method in fragment and call that using instance of that fragment which you have already replace/add. – Nitin Patel Jul 11 '17 at 09:21