Bloopie Bloops Bloopie Bloops - 6 months ago 25
Android Question

Broadcastreceiver cannot receive when activity not opened

I have a notepad activity that opens up a window dialog version of it where user can go to another app and still type on the notepad

For both notepad to have the same content, I used broadcastReceiver to set the text of the notepad activity to the one in the dialog when the user is done.

Activity's BroadcastReceiver:

private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final globalVariable globalVariable = (globalVariable) getApplicationContext();
et_editor.setText(globalVariable.getScriptEditorText());
Toast.makeText(Script_editor.this, "Script Editor have been updated", Toast.LENGTH_SHORT).show();
}
};


Close Button of the Dialog:

btn_Close = (Button) view.findViewById(R.id.button_Close);
btn_Close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

AlertDialog alertDialog = new AlertDialog.Builder(getApplicationContext())
.setTitle("Would you like to update the editor?")
.setNegativeButton("No, discard the changes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(ColorCoordinatePickerService.this, "Changes have been discarded", Toast.LENGTH_SHORT).show();
dialog.dismiss();
stopSelf();
}
})
.setPositiveButton("Yes, update with changes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
final globalVariable globalVariable = (globalVariable) getApplicationContext();
globalVariable.setScriptEditortext(CCEditor.getText().toString());
Intent intent = new Intent(BROADCAST_ACTION);
sendBroadcast(intent);

dialog.dismiss();
stopSelf();
}
})
.create();

alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
alertDialog.show();
}
});


It works BUT the user have to go back to the notepad activity (cannot be in another app or activity).

How do I update the notepad's content without having to "open" it up?

Answer

Queue the changes in BroadcastReceiver and when NotepadActivity resumes set the changes in the EditText. The queue doesn't need to be an actual queue, you can persist the last broadcast receiver data somewhere.

Comments