java - Finish All Activities from BroadCast Receiver -
i want finish applications' activities broadcast receiver when screen off: first created static reference in activities below:
public class mainactivity extends appcompatactivity implements navigationview.onnavigationitemselectedlistener { public static appcompatactivity ma; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //set instance of ma equals mainactivity ma=this; toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); floatingactionbutton fab = (floatingactionbutton) findviewbyid(r.id.fab); fab.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { snackbar.make(view, "replace own action", snackbar.length_long) .setaction("action", null).show(); } }); drawerlayout drawer = (drawerlayout) findviewbyid(r.id.drawer_layout); actionbardrawertoggle toggle = new actionbardrawertoggle( this, drawer, toolbar, r.string.navigation_drawer_open, r.string.navigation_drawer_close); drawer.setdrawerlistener(toggle); toggle.syncstate(); navigationview navigationview = (navigationview) findviewbyid(r.id.nav_view); navigationview.setnavigationitemselectedlistener(this); } @override public void onbackpressed() { drawerlayout drawer = (drawerlayout) findviewbyid(r.id.drawer_layout); if (drawer.isdraweropen(gravitycompat.start)) { drawer.closedrawer(gravitycompat.start); } else { } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } public void displayview(int viewid) { fragment fragment = null; string title = getstring(r.string.app_name); switch (viewid) { case r.id.monthly_figures: fragment = new dailysales(); title = "daily figures"; break; case r.id.nav_gallery: fragment=new monthlysales(); title="monthly figures"; break; } if (fragment != null) { fragmenttransaction ft = getsupportfragmentmanager().begintransaction(); ft.replace(r.id.content_frame, fragment); ft.commit(); } // set toolbar title if (getsupportactionbar() != null) { getsupportactionbar().settitle(title); } drawerlayout drawer = (drawerlayout) findviewbyid(r.id.drawer_layout); drawer.closedrawer(gravitycompat.start); }
then call broadcastreceiver class below:
public class scbroadcaster extends broadcastreceiver { public void onreceive(context context, intent intent) { if(intent.getaction().equals(intent.action_screen_off)){ mainactivity.ma.finish(); } } }
do thinks there better way? tried call system.exit(0) or process.killprocess(mypid) when screen turned on restart whole application. thanks.
android isn't set this. framework's idea app should never close , reopenable same spot. think bad idea (some apps need close security reasons after timeout), that's how works.
as hate them, think event bus right way go here. have every activity register event bus event in oncreate. have receiver send event when detects screen off (or whatever other trigger want). when activity receives event, have call finish(). make things easier, code behavior base activity class , have activities derive class instead of activity directly.
Comments
Post a Comment