GNLauncher - noxiouswinter/gnlib_android GitHub Wiki

GNLauncher makes sending objects/data to an Activity from another Activity etc as easy as calling a function in the Activity with the required data as parameters. It introduces type safety and removes all the hassles of having to serialize, attaching to the intent using string keys and undoing the same at the other end.

You can also trigger different functionalities in the Activity directly by choosing which method to launch into with the data.

Usage

Define an interface with the methods you want to call on the Activity to launch.

public interface IPayload {
    public void sayHello(String name, int age);
}

Implement the above interface on the Activity to launch into. Also in onCreate, notify GNLauncher when the Activity is ready to be called into.

public class Activity_1 extends Activity implements IPayload {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Notify GNLauncher when the Activity is ready. 
        GNLauncher.get().ping(this);
    }

    @Override
    public void sayHello(String name, int age) {
        Log.d("gnlib_test", "Hello " + name + "! \nYour age is: " + age);
    }
}

In the other Activity(or anywhere outside Activity_1 and with a context), get a proxy to the above Activity and call any method on the proxy with the desired parameters.

public class Activity_2 extends Activity {
    public void onClick(View v) {
        GNLauncher launcher = GNLauncher.get();
        IPayload proxy = (IPayload)launcher.getProxy(this, IPayload.class, Activity_1.class);
        proxy.sayHello(name, age);
    }
}

The first Activity will be launched and the method called into with the required parameters.