1

I tried to learn LocalBroadcastManager. Searched in web and implemented a simple app with Two activity to perform LocalBroadcastManager. But it's not working. I can't found what's wrong with my code. please help me!

here is my code.

FirstActivity.java

public class FirstActivity extends Activity {

private Button button;

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

    button = (Button) findViewById(R.id.button);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(FirstActivity.this, SecondActivity.class));
            Intent intent = new Intent("localReceiver");
            LocalBroadcastManager.getInstance(FirstActivity.this).sendBroadcast(intent);
        }
    });
}    }

SecondActivity.java

public class SecondActivity extends Activity {

private TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);
    textView = (TextView) findViewById(R.id.hi_text);
    LocalBroadcastManager.getInstance(this).registerReceiver(message, new IntentFilter("localReceiver"));
}

@Override
protected void onPause() {
    super.onPause();
    LocalBroadcastManager.getInstance(this).unregisterReceiver(message);
}

private BroadcastReceiver message = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("SecondActivity", "Receiver Initiated...");
        textView.setText("Intent receiver activated");
    }
};   }

AndroidManifest.xml

    <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".FirstActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name=".SecondActivity"
        android:windowSoftInputMode="adjustResize|stateHidden"
        android:label="SecondActivity" />

</application>
khaleel_jageer
  • 1,404
  • 4
  • 16
  • 37
  • 2
    how can you be at the same time on `FirstActivity` and `SecondActivity` ? – Blackbelt Jul 09 '15 at 09:10
  • @Blackbelt pls explain? – khaleel_jageer Jul 09 '15 at 09:15
  • 1
    I think what @BlackBelt is saying is that is yout on `FirstActivity` but that `SecondActivity` is not running, there is no way that the receiver from second activity is registered, and so it is normal that nothing happend when you send intent. – sonic Jul 09 '15 at 09:28

2 Answers2

2

you are broadcasting something from FirstActivity, and you expect that SecondActivity receives it. In order this to happen you should be able to run two different Activitys at the same time, which is, by design, not possible. By the time you broadcast the string, you don't have anybody listening for it. Try using a Service to broadcast the String, with the receiving Activity resumed, and it will work

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
1

SecondActivity would looks like this:

private BroadcastReceiver uiUpdateReceiver; 

onStart():

@Override
protected void onStart() {
    LocalBroadcastManager.getInstance(SecondActivity.this).registerReceiver((uiUpdateReceiver), new IntentFilter("Your_Data");
    super.onStart();
}

onCreate():

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

    uiUpdateReceiver = new BroadcastReceiver() {
           @Override
           public void onReceive(Context context, Intent intent) {

           }
     };
}

onStop():

@Override
protected void onStop() {
    LocalBroadcastManager.getInstance(ChatActivity.this).unregisterReceiver(uiUpdateReceiver);
    super.onStop();
}

Hope it will help you.

Hiren Patel
  • 52,124
  • 21
  • 173
  • 151
  • If you need to change views or replace fragments, you need to register it in onResume() and unregister in onPause(), otherwise it can throw an “IllegalStateException: Can not perform this action after onSaveInstanceState”. This will also cut down on unnecessary system overhead. – heloisasim Aug 21 '16 at 19:26