活动到活动回调监听器

问题描述 投票:3回答:4

让我们假设有2个活动Activity1Activity2。我需要从methodAct1()(在Activity1内部)调用方法methodAct2(在Activity2内)。我认为它应该使用回调监听器 - 我不想使用EventBus库!

我使用以下代码获得java.lang.NullPointerException

接口:

public interface MyListener {
    public void listen();
}

创建事件的活动:

public class Activity2 extends Activity {

    private MyListener  myListener;

    public void setUpListener(MyListener myListener) {
        this.myListener = myListener;
    }

    private void doWork(){
        //do stuff 
        myListener.listen();
    }
}

我希望在工作完成后获得该活动的活动:

public class Activity1 extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Activity2 activity2 = new Activity2();
        activity2.setUpListener(new setUpListener() {
            @Override
            public void listen() {
                // get the event here

            }
        });
    }
}
java android callback listener
4个回答
3
投票

这绝对不可能。您永远不会自己实例化新活动。您不会同时运行两个活动。

如果您希望其他Activity根据您之前的Activity需要执行某些操作,则需要将其添加到您的Intent中。

Intent intent = new Intent(this, Activity2.class);
intent.putExtra("data field", "data value");
startActivity(intent);

如果您想通过回调获得特定功能,那么您可能会想到Fragments。通过这种方式,您可以运行相同的Activity,它可以告诉单个Fragments他们需要做什么。


3
投票

由于您的陈述,NPE正在发生:

Activity2 activity2 = new Activity2(); <--

你永远不应该这样做,而你应该在活动1中做:

Intent intent = new Intent(this, Activity2.class);
intent.putExtra("dataKey", "dataValue");
startActivityForResult(pickContactIntent, CALLBACK_REQUEST);

startActivityForResult()提供从Activity 2到Activity 1的回调,你必须覆盖activity 1中的结果:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Check which request we're responding to
    if (requestCode == CALLBACK_REQUEST) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {
            // The Intent's data Uri identifies which contact was selected.

            // Do something with the contact here (bigger example below)
        }
    }
}

1
投票

你可以这样做你的方法....

public class Activity2 extends AppCompatActivity {

    private static MyListener  myListener;

    public static void setUpListener(MyListener Listener) {
        myListener = Listener;
    }

    public void doWork(View view) {
        myListener.listen();
    }
}


public class Activity1 extends AppCompatActivity {

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

        Activity2.setUpListener(new MyListener() {
            @Override
            public void listen() {
                Log.d("Hello", "Hello World");
            }
        });
    }

    public void goToAnotherActivity(View view) {
        startActivity(new Intent(Activity1.this, Activity2.class));
    }
}

虽然这不是最佳方法,但为了使用此机制,您需要创建activity1。


0
投票

您可以这样做> - 步骤01:实现共享接口


public interface SharedCallback {
    public String getSharedText(/*you can define arguments here*/);
}
  • 步骤02:实现共享类

final class SharedMethode {
    private static Context mContext;

    private static SharedMethode sharedMethode = new SharedMethode();

    private SharedMethode() {
        super();
    }

    public static SharedMethode getInstance() {
        return sharedMethode;
    }

    public void setContext(Context context) {
        if (mContext != null)
            return;

        mContext = context;
    }

    public boolean contextAssigned() {
        return mContext != null;
    }

    public Context getContext() {
        return mContext;
    }

    public void freeContext() {
        mContext = null;
    }
}

- 步骤03 ::在第一个活动中播放代码


public class FirstActivity extends Activity implements SharedCallback {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.your_layout);

        // call playMe from here or there
        playMe();
    }

    private void playMe() {
        SharedMethode.getInstance().setContext(this);
        Intent intent = new Intent(this, SecondActivity.class);
        startActivity(intent);
    }

    @Override
    public String getSharedText(/*passed arguments*/) {
        return "your result";
    }

}
  • 步骤04 ::在SecondActivity中完成游戏

public class SecondActivity extends Activity {

    private SharedCallback sharedCallback;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.your_layout);

        if (SharedMethode.getInstance().contextAssigned()) {
            if (SharedMethode.getInstance().getContext() instanceof SharedCallback)
                sharedCallback = (SharedCallback) SharedMethode.getInstance().getContext();

            // to prevent memory leak. no further needs
            SharedMethode.freeContext();
        }

        // You can now call your implemented methodes from anywhere at any time
        if (sharedCallback != null)
            Log.d("TAG", "Callback result = " + sharedCallback.getSharedText());

    }

        @Override
        protected void onDestroy() {
        sharedCallback = null;
        super.onDestroy();
    }

}

您还可以实现向后回调(从First到Second)以从Second Activity获取一些结果或调用某些方法

© www.soinside.com 2019 - 2024. All rights reserved.