Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
359 views
in Technique[技术] by (71.8m points)

java - How to use interface to communicate between two activities

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.

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...