android - How to close app/activity or code menu item via intent/pendingIntent in Chrome Custom Tabs -
i'm trying understand how chrome custom tabs work. following guidelines, adding custom item menu done using pendingintent, such as:
intent intent = new intent(intent.action_view, uri.parse("http://google.com")); pendingintent pendingintent = pendingintent .getactivity(this, 0, intent, pendingintent.flag_update_current); customtabsintent = new customtabsintent.builder() .addmenuitem("visit google website", pendingintent) .build();
i know how use pendingintent direct website or create 'share' action. question possible more complex tasks adding 'close' menu item close activity or exit app; or 'connect' menu item method, when user clicks on code method process. guess second if possible make easy solution first.
i tried search answers, if search how close or end activity or app (or similar) using intent or pendingintent, use of 'finish();' or 'startactivity(intent);' or similar, cannot apply here since don't know how or call it. tried reading use of intent , pendingintent, cannot figure out how to, let's close activity or process without being able call method.
i appreciate help, links resources or pointers need read/learn next able understand this.
this solution using brodcastreceiver:
in activity (mainactivity in case) starting chrome custom tabs (cct), create static field identify if activity created broadcastreceiver, , setter can set broadcastreceiver:
// determining if activity started broadcastreceiver private static boolean frombroadcastreceiver = false; public static void setfrombroadcastreceiver(boolean bool) { frombroadcastreceiver = bool; }
create public class broadcastreceiver. overriding onreceive method, create instance of mainactivity , set frombroadcastreceiver
true. create intent activity , intent restarting using first intent. close cct , restart activity using latter intent:
import android.content.broadcastreceiver; import android.content.componentname; import android.content.context; import android.content.intent; import android.support.v4.content.intentcompat; public class cctbroadcastreceiver extends broadcastreceiver { @override public void onreceive(context context, intent intent) { // create instance of mainactivity , set frombroadcastreceiver flag true. mainactivity mainactivity = new mainactivity(); mainactivity.setfrombroadcastreceiver(true); // create intent activity , intent restarting using first intent. intent intent = new intent(context, mainactivity.getclass()); componentname compname = intent.getcomponent(); intent mainintent = intentcompat.makerestartactivitytask(compname); // restart activity using later intent (also close cct) mainintent.addflags(intent.flag_activity_clear_top | intent.flag_activity_new_task); mainintent.addflags(intent.flag_activity_no_history); context.startactivity(mainintent); } // end onreceive. } // end broadcastreceiver class.
don't forget register receiver in androidmanifest.xml:
... </activity> <receiver android:name=".cctbroadcastreceiver" android:enabled="true"> </receiver> </application>
now, inside oncreate
of mainactivity check if created broadcastreceiver, , if reset 'flag' , finish()
activity:
// check if activity created broadcastreceiver, , if reset 'flag' , finish() activity. if (frombroadcastreceiver) { setfrombroadcastreceiver(false); finish(); return; }
don't forget use broadcastreceiver class creating intent , pendingintent cct:
intent broadcastintent = new intent(this, cctbroadcastreceiver.class); pendingintent pendingintent = pendingintent.getbroadcast(this, 0, broadcastintent, pendingintent.flag_update_current);
and you're done. code inside broadcastreceiver closes cct (similar if clicked on cct default close button). adding 'flag' , code inside mainactivity further closes mainactivity.
Comments
Post a Comment