Home > OS >  how to invoke an Activity(Java Class ) method from Fragment?
how to invoke an Activity(Java Class ) method from Fragment?

Time:09-22

how can i call or invoke Regular Activity class method from a fragment. I have tried to invoke Registration class method activityFunction from fragment like below and it returns null pointer exception. below is the entire fragment class and any help would be highly appreciated. i have googled around and it has been mentioned that i have to manually assign fragment to activity and then call the method , but i sort of couldnt find a way to do that . Any help would be highly appreciated. Below in the code i was trying to call activity method from fragment on clicking a button.

Registration media = new Registration(); media.activityFunction();

 public class fragment_login extends Fragment  {
         private static final String ARG_PARAM1 = "param1";
            private static final String ARG_PARAM2 = "param2";
        
            // TODO: Rename and change types of parameters
            private String mParam1;
            private String mParam2;
            public fragment_login() {
                // Required empty public constructor
            }
            public static fragment_login newInstance(String param1, String param2) {
                fragment_login fragment = new fragment_login();
                Bundle args = new Bundle();
                args.putString(ARG_PARAM1, param1);
                args.putString(ARG_PARAM2, param2);
                fragment.setArguments(args);
                return fragment;
            }


           @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                     Bundle savedInstanceState) {
                // Inflate the layout for this fragment
               View v = inflater.inflate(R.layout.fragment_login, container, false);
               login = v.findViewById(R.id.et_email);
               password = v.findViewById(R.id.et_password);
                send = v.findViewById(R.id.btn_login);
                 return v;
            }



         // thats where i am trying to call clas method from fragment 
            @Override
            public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        
                send.setOnClickListener(new View.OnClickListener() {
              
                    Registration media = new Registration();
                    media.activityFunction();
        
                    }
                });
            }

CodePudding user response:

You can use Local Broadcast Receiver to send the event from fragment to activity and do the work that you want to do from fragment. For Example

Fragment.java

    LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(context);
    Intent localIntent = new Intent(“CUSTOM_ACTION”);
    intent.putExtra("key", "value");
    localBroadcastManager.sendBroadcast(localIntent);

Activity.java

    private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
    public void onReceive( Context context, Intent intent ) {
        String data = intent.getStringExtra(“key”);
        Log.d( “Received data : “, data);
    }
};

register broadcast receiver in activity in onResume() and unregister in onStop().

register using below code

IntentFilter filter = new IntentFilter("CUSTOM_ACTION");
    registerReceiver(receiver, filter);

unregister using below code:

localBroadcastManager.unregisterReceiver(receiver);

Note: Don't forget to use the same action as declared while sending broadcast.

CodePudding user response:

You can call activity method like this, if fragment is loaded in that activity

((MainActivity)getActivity()).abcMethod();
  • Related