gpt4 book ai didi

java - 如何使用接口(interface)在两个 Activity 之间进行通信

转载 作者:IT老高 更新时间:2023-10-28 20:30:23 25 4
gpt4 key购买 nike

我正在尝试在两个 Activity Act1 和 Act2 之间创建监听器接口(interface)。 Act1 将启动 Act2。如果Act2中发生了一些事件,它会通知Act1。问题是我正在使用 Intent 开始新的 Activity ,那么 Act1 将如何将自己分配为 Act2 接口(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);
}
}

注意:请不要建议我使用 fragment 。我目前想使用 Activity 。

最佳答案

我建议创建一个 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();
}
}

你将如何使用它:

// 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));
}
}

在第二个 Activity 中更改成员(member)状态:

// Imports
public class SecondActivity extends Activity {

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

LogCat 输出:

当前状态:假

MainActivity 说:模型状态已更改:true

关于java - 如何使用接口(interface)在两个 Activity 之间进行通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19026515/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com