How to use interface to communicate between two activities

NightFury picture NightFury · Sep 26, 2013 · Viewed 38.1k times · Source

I am trying to make listener interface between two activities Act1 and Act2. Act1 will start Act2. If there is some event occurred in Act2, it will inform it to Act1. Problem is that I am starting new activity using Intent, so how Act1 will assign itself as listener to Act2's interface?

Act1.java

public class Act1 extends ActionBarActivity implements
        ActionBar.OnNavigationListener {

    ActionBar actionbar;
    Intent pizzaIntent;
    boolean visibleFirstTime = true;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menutab);

        //set actionbar here
    }

@Override
    public boolean onNavigationItemSelected(int arg0, long arg1)// item pos,
                                                                // itemid
    {
        switch (arg0) {
        case 0:
            if(this.visibleFirstTime == false)
            {
            if(pizzaIntent == null)
            {
                pizzaIntent = new Intent(this,Act2.class);
                //how to call setChangeListener?
            }
            startActivity(pizzaIntent);
            }
            else
                this.visibleFirstTime = false;
            break;
        case 1:
            System.out.println("selected: " + arg0);
            break;
        case 2:
            System.out.println("selected: " + arg0);
            break;
        case 3:
            System.out.println("selected: " + arg0);
            break;
        default:
            break;
        }
        return true;
    }
}

Act2.java

public class Act2 extends Activity {

     selectionChangeListener listener;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pizza_slice_selection);
    }

    public void setChangeListener(selectionChangeListener listener)
    {
        this.listener = listener;
    }

    private interface selectionChangeListener
    {
        public void selectionMadeAtIndex(int index);
    }
}

Note: Please don't suggest me to use fragments. I want to use activities currently.

Answer

vilpe89 picture vilpe89 · Sep 26, 2013

I would suggest to create a model class. Let me give you an example:

Model class:

public class CustomModel {

    public interface OnCustomStateListener {
        void stateChanged();
    }

    private static CustomModel mInstance;
    private OnCustomStateListener mListener;
    private boolean mState;

    private CustomModel() {}

    public static CustomModel getInstance() {
        if(mInstance == null) {
            mInstance = new CustomModel();
        }
        return mInstance;
    }

    public void setListener(OnCustomStateListener listener) {
        mListener = listener;
    }

    public void changeState(boolean state) {
        if(mListener != null) {
            mState = state;
            notifyStateChange();
        }
    }

    public boolean getState() {
        return mState;
    }

    private void notifyStateChange() {
        mListener.stateChanged();
    }
}

And here's how you would use this:

// Imports
public class MainActivity extends Activity implements OnCustomStateListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        CustomModel.getInstance().setListener(this);

        boolean modelState = CustomModel.getInstance().getState();
        Log.d(TAG, "Current state: " + String.valueOf(modelState));

        Intent intent = new Intent(this, SecondActivity.class);
        startActivity(intent);
    }

    @Override
    public void stateChanged() {
        boolean modelState = CustomModel.getInstance().getState();
        Log.d(TAG, "MainActivity says: Model state changed: " + 
            String.valueOf(modelState));
    }
}

Changing the member state in second activity:

// Imports
public class SecondActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        CustomModel.getInstance().changeState(true);
    }
}

LogCat output:

Current state: false

MainActivity says: Model state changed: true