Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Of course it will not work when Dev Options->Don't Keep Activitie is on
package pl.selvin.completelywrongsolution;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class DoNotDoThisBecauseItsStupid {
public static class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button btn = new Button(this);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, SecondActivity.class));
}
});
btn.setText("MainActivity - open SecondActivty");
setContentView(btn);
registerReceiver(broadcast_reciever, new IntentFilter("finish_activity"));
}
BroadcastReceiver broadcast_reciever = new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent intent) {
String action = intent.getAction();
if (action.equals("finish_activity")) {
Log.e("OnReceive!!", "allo, allo");
finish();
}
}
};
@Override
protected void onDestroy() {
super.onDestroy();
//It is bad to do not unregister but screw that solution is wrong so we don't do this ... it's still would not work
//unregisterReceiver(broadcast_reciever);
}
}
public static class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button btn = new Button(this);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("finish_activity");
sendBroadcast(intent);
finish();
}
});
btn.setText("SecondActivity - close MAIN");
setContentView(btn);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.