本文介绍了如何使用界面两项活动之间的通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出两个活动 ACT1和ACT2 之间的侦听器接口。 ACT1 将启动 ACT2 。如果在 ACT2 某些事件发生时,它会通知到 ACT1 。问题是,我开始新的活动使用的意图,所以ACT1如何分配自身作为监听器ACT2的接口?

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.

推荐答案

你有没有考虑使用LocalBroadcastManager?

在ACT1的onCreate:

In Act1's onCreate:

act2InitReceiver= new BroadcastReceiver()
    {

        @Override
        public void onReceive(Context context, Intent intent)
        {
            // do your listener event stuff
        }
    };
LocalBroadcastManager.getInstance(this).registerReceiver(act2InitReceiver, new IntentFilter("activity-2-initialized"));

在ACT1的的onDestroy:

In Act1's onDestroy:

LocalBroadcastManager.getInstance(this).unregisterReceiver(act2InitReceiver);

在ACT2的onCreate:

In Act2's onCreate:

LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("activity-2-initialized"));

给我一个评论,如果code不能编译,我用手写的。

Give me a comment if the code doesn't compile, I'm writing it by hand.

这篇关于如何使用界面两项活动之间的通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 23:15