Andy
Andy

Reputation: 21

Broadcast Send/Receive between Activities

New to Android and trying to implement Broadcast communication between two Activities. I've looked at many examples in books, web sites, and have followed several threads here, but I am still missing something. Most samples have an Activity which extends from BroadcastActivity, which I believe this NOT what is needed for my app. I put together a straight forward sample to test.

What am I missing?? The entire code is below.

Thanks for your help.

package com.dialogtest.dt;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class DialogActivity extends Activity {

        public static final String DTAG = "ADebug";

        public static String strDia = "declr";

        public static String BroadMess = "com.dialogtest.dt.Restart";
        public static TextView tv;
    }

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends DialogActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        
        setContentView(R.layout.main);

        strDia = "One";

        tv = (TextView) findViewById(R.id.TextView_Main);
        final Button diaButton = (Button) findViewById(R.id.Button_A);

        diaButton.setOnClickListener(new View.OnClickListener() {           
           @Override
           public void onClick(View v) {

              startActivity(new Intent(MainActivity.this, TestActivity.class));                                 
        }   
     });                                    
    }

    BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {         
            tv.setText("SUCCESS - RECEIVED!");
            Log.e(DTAG, "in Main - RECEIVED BROADCAST - str: " + strDia);
        }
    };

    @Override
    protected void onResume() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(BroadMess);
        registerReceiver(receiver, filter);

        super.onResume();
    }

    @Override
    protected void onPause() {
        unregisterReceiver(receiver);

        super.onPause();
    }        
}

public class TestActivity extends DialogActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        
        setContentView(R.layout.two_dialog);

       final Button dB = (Button) findViewById(R.id.ButtonDone);       
       dB.setOnClickListener(new View.OnClickListener() {           
       @Override
       public void onClick(View v) {
           finish();
       }    
     });     
    }

    @Override
    public void onStart() {
        super.onStart();

        EditText dET = (EditText) findViewById(R.id.EditText01);       
        dET.setText(strDia);
    }

    @Override
    public void onPause() {
        super.onPause();

        EditText dET = (EditText) findViewById(R.id.EditText01);
        strDia = dET.getText().toString();

        Intent broadcast = new Intent();
        broadcast.setAction(BroadMess);
        sendBroadcast(broadcast);

        Log.e(DTAG, "in Test onPause Sending Broadcast - strDia: " + strDia);
    }

}

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.dialogtest.dt"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:label="@string/app_name" android:name="MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    <activity android:name="DialogActivity"></activity>
<activity android:name="TestActivity" android:theme="@android:style/Theme.Dialog"></activity>
</application>
    <uses-sdk android:minSdkVersion="3" />
</manifest>

Upvotes: 1

Views: 7405

Answers (1)

Cheryl Simon
Cheryl Simon

Reputation: 46844

It does not make sense to use broadcast to communicate between two Activities. Only one of the Activities will ever be active at a time. It does make sense to use it to communicate between an Activity and a Service, for example.

I don't know what you are trying to communicate, but generally you can use intent extras to pass information to the next Activity. I.e, using putExtra.

(java.lang.String, android.os.Bundle)

Upvotes: 4

Related Questions