Skip to content

Instantly share code, notes, and snippets.

@SelvinPL
Last active August 29, 2015 14:25
Show Gist options
  • Select an option

  • Save SelvinPL/620e6c05cd59dc7c16c2 to your computer and use it in GitHub Desktop.

Select an option

Save SelvinPL/620e6c05cd59dc7c16c2 to your computer and use it in GitHub Desktop.
LocalBroadcastManager for communication between activities is bad, m'kay!
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "pl.selvin.android.localbroadcastreceivertest"
minSdkVersion 4
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
}
}
}
dependencies {
compile 'com.android.support:support-v4:22.2.1'
}
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="pl.selvin.android.localbroadcastreceivertest">
<application android:allowBackup="true" android:label="localbroadcastreceivertest" >
<activity android:name=".Activities$ReceiverActivity" android:label="!test">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Activities$SenderActivity"/>
</application>
</manifest>
package pl.selvin.android.localbroadcastreceivertest;
import android.app.Activity;
import android.app.ListActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
public class Activities {
public static final String EVENT = "custom-event-name";
public static final String MESSAGE = "message";
public static class ReceiverActivity extends ListActivity {
final static String TAG = SenderActivity.class.getName();
public ArrayAdapter<String> adapter;
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String message = intent.getStringExtra(MESSAGE);
Log.d(TAG, "Got message: " + message);
if(adapter!=null)
adapter.add(message);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, new String[] { "Item1", "Item2", "Item3", "Item4", "Item5"});
setListAdapter(adapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
final Intent intent = new Intent(this, SenderActivity.class);
startActivity(intent);
}
@Override
protected void onResume() {
super.onResume();
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
new IntentFilter(EVENT));
}
@Override
protected void onPause() {
//without this we are in deep shit
//with this we will not receive the broadcast
//LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
super.onPause();
}
@Override
protected void onDestroy() {
LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
super.onDestroy();
}
}
public static class SenderActivity extends Activity {
final static String TAG = SenderActivity.class.getName();
int counter = 6;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Button but = new Button(this);
but.setText("Click me!");
but.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "Broadcasting message");
Intent intent = new Intent(EVENT);
intent.putExtra(MESSAGE, "Item" + counter);
LocalBroadcastManager.getInstance(SenderActivity.this).sendBroadcast(intent);
counter ++;
}
});
setContentView(but);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment