简体   繁体   中英

How to use EventBus in two Activity on Android

In my application i want use EventBus for manage events.
In my app i open activity B on the Activity A ! and i want when activity B finished, call one event in Activity A .

I write below codes

Activity B :

private void finishWithAnimate() {
    EventBus.getDefault().post(new EventShowDialog(ExtraConstants.NOTE_DIALOG.name()));
    if (pageType.equals(Extras.DETAIL)) {
        Intent intent = new Intent();
        setResult(4757, intent);
        finish();
    } else {
        finish();
        RegisterInAuctionActivity.this.overridePendingTransition(0, android.R.anim.fade_out);
    }
}

Activity A :

@Subscribe(threadMode = ThreadMode.MAIN)
public void getEventShowDialog(EventShowDialog eventShowDialog) {
    Log.e("showDialogEvent", "Root");
    if (eventShowDialog.getDialogName().equals(ExtraConstants.NOTE_DIALOG.name())) {
        Log.e("showDialogEvent", "Call");
        if (!prefsUtils.getFromSharedBool(PrefsKeys.NEW_NOTE_DIALOG.name())) {
            closeDialog.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    noteDialog.dismiss();
                    prefsUtils.setToSharedBool(PrefsKeys.NEW_NOTE_DIALOG.name(), true);
                }
            });
            noteDialog.show();
        }
    }
}

@Override
public void onStart() {
    super.onStart();
    EventBus.getDefault().register(this);
}

@Override
public void onStop() {
    EventBus.getDefault().unregister(this);
    super.onStop();
}

Update codes:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 4757) {
        //Note dialog
        noteDialog = new Dialog(this);
        noteDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        noteDialog.setContentView(R.layout.important_note);
        noteDialog.setCancelable(false);
        closeDialog = noteDialog.findViewById(R.id.okPayFrag_submitOK);
        closeDialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                noteDialog.dismiss();
                prefsUtils.setToSharedBool(PrefsKeys.NEW_NOTE_DIALOG.name(), true);
            }
        });
        if (!prefsUtils.getFromSharedBool(PrefsKeys.NEW_NOTE_DIALOG.name())) {
            Toast.makeText(context, "Show", Toast.LENGTH_SHORT).show();
            noteDialog.show();
            closeDialog.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    noteDialog.dismiss();
                    prefsUtils.setToSharedBool(PrefsKeys.NEW_NOTE_DIALOG.name(), true);
                }
            });
        }
        finish();
        overridePendingTransition(android.R.anim.fade_in, 0);
        startActivity(getIntent());
        overridePendingTransition(android.R.anim.fade_in, 0);
    }
}

But not call any log in getEventShowDialog . Not show me any log in this method!

how can i fix it?

You are trying to perform some actions on getting a result from an activity. So when your activity B finishes, do a setResult and catch the same on Activity A onActivityResult and perform your action(call the function you want). You need to do a startActivityForResult while opening B from A though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM