2

I would like to perform some actions on an Activity after a onClick event on a Fragment.

I only found how to communicate between a fragment and an Activity to which the fragment is attached by using getActivity(), which is NOT my case.

APagano
  • 108
  • 5

4 Answers4

3

For a super simple solution use the fragment to trigger a method in your activity class that uses an intent to call the activity required.

EDIT: Check the comments below for more help.

KISHORE_ZE
  • 1,466
  • 3
  • 16
  • 26
0

As others said, you have to use broadcasts or services.....

but:

The easiest and fastest way to do it is:

create these two classes::

globals:

public class Globals {

private static final Globals instance = new Globals();
private GlobalVariables globalVariables = new GlobalVariables();

private Globals() {
}

public static Globals getInstance() {
    return instance;
}

public GlobalVariables getValue() {
    return globalVariables;
}

}

and global variables ::

public class GlobalVariables {
    public Activity receiverActivity;
}

not in receiver activity::

Globals.getInstance().getValue().receiverActivity = this;

and sender can do anything now! for example calling a public method from receiver activity:

Globals.getInstance().getValue().doSomething();

It's not recommended basically, but works great. :D

Sina KH
  • 563
  • 4
  • 16
  • 1
    "It's not recommended basically [..]" right - don't do it then. Use aBroadcastReceiver. Global variables...just don't! – JacksOnF1re Aug 19 '15 at 10:24
0

You have to create an interface which your activity implements. BroadcastReceivers do have their advantages but solving this issue with callbacks is straight forward and easy to do.

Here is an example. We give our Activity a String and have it return a Integer:

public interface IMyStringListener{
public Integer computeSomething(String myString);
}

The interface can be defined in the fragment or in a separate file. Next your Activity implement the Interface.

public class MyActivity implements IMyStringListener{

 @Override
 public Integer computeSomething(String myString){
   /** Do something with the string and return your Integer instead of 0 **/ 
   return 0;
  }

}

Then in your fragment you would have a MyStringListener variable and you would set the listener in fragment onAttach(Activity activity) method.

public class MyFragment {

    private MyStringListener listener;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            listener = (MyStringListener) activity;
        } catch (ClassCastException castException) {
            /** The activity does not implement the listener. */
        }
    }

}
Mibit
  • 316
  • 1
  • 10
  • OP said that his fragment is not attached to the activity he needs to communicate with and there is no other connection. – JacksOnF1re Aug 19 '15 at 10:36
  • The Fragment is able to communicate with ANY Activity, as long the Activity implements the Interface. It decouples your Fragments from a particular implementation of an Activity. – Mibit Aug 19 '15 at 10:55
  • It is only an interface, therefore you need a reference of the interface, which OP does *not* have in his implementation. – JacksOnF1re Aug 19 '15 at 12:10
  • this: "listener = (MyStringListener) activity;" won't work. Because the activity in onAttach is not the activity that should be called. – JacksOnF1re Aug 19 '15 at 12:11
0

use delegation: one createView trigger the delegate, and create listener in the activity

try this : Fragment-Fragment communication in Android

just the delegate function

Community
  • 1
  • 1
Wael Abo-Aishah
  • 942
  • 2
  • 10
  • 27