khaleel_jageer
khaleel_jageer

Reputation: 1502

Why LocalBroadcastManager is not working?

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>

Upvotes: 1

Views: 2237

Answers (2)

Hiren Patel
Hiren Patel

Reputation: 52800

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.

Upvotes: 1

Blackbelt
Blackbelt

Reputation: 157457

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

Upvotes: 2

Related Questions