Using android:action to launch activity from settings -


i have defined settings.xml file include item uses android:action item in setting dialog. see sample code activity below. works fine. thing "overlaying" entire activity , when user presses button entire application finishes. there way launch "fragment" using android:action in settings.xml or how can restructure activity when activity finishes main activity resumed?

<preferencescreen>     <preference android:title="current user" >         <intent android:action="com.example.coreui.showcurrentuseractivity"         />     </preference> </preferencescreen> 

here activity code

public class showcurrentuseractivity extends activity {     public  alertdialog dialog = null;     @override     public void oncreate(bundle savedinstancestate) {         string msgstr;         super.oncreate(savedinstancestate);         alertdialog.builder alert = new alertdialog.builder(this);         alert.setpositivebutton("logout",         new dialoginterface.onclicklistener() {             public void onclick(dialoginterface dialog, int id) {                     dialog.dismiss();                     showcurrentuseractivity.this.finish();             }         });         alert.setnegativebutton("dismiss",             new dialoginterface.onclicklistener() {                 public void onclick(dialoginterface dialog, int id) {                     dialog.dismiss();                     showcurrentuseractivity.this.finish();             }         });     } } 

this how specifiy activity in androidmanifest.xml

    <activity         android:name="com.example.coreui.showcurrentuseractivity"         android:label="currentuser"         android:exported="false">          <intent-filter>             <action android:name="com.example.coreui.showcurrentuseractivity" />             <category android:name="android.intent.category.default" />         </intent-filter>     </activity> 

it works fine if dialog activity itself. possible create activity showing fragment. (i'll edit answer soon.)

androidmanifest.xml

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.gyebro.settingsintent" >  <application     android:allowbackup="true"     android:icon="@drawable/ic_launcher"     android:label="@string/app_name"     android:theme="@style/apptheme" >     <activity         android:name=".showcurrentuseractivity"         android:label="show current user"         android:theme="@android:style/theme.holo.light.dialog">         <intent-filter>             <action android:name="com.gyebro.settingsintent.show_current_user" />             <category android:name="android.intent.category.default" />         </intent-filter>     </activity>     <activity         android:name=".settingsactivity"         android:label="@string/title_activity_settings" >         <intent-filter>             <action android:name="android.intent.action.main" />             <category android:name="android.intent.category.launcher" />         </intent-filter>     </activity> </application> </manifest> 

settingsactivity.java

public class settingsactivity extends preferenceactivity { @override public void onbuildheaders(list<header> target) {     loadheadersfromresource(r.xml.pref_headers, target); } } 

pref_headers.xml

<preference-headers xmlns:android="http://schemas.android.com/apk/res/android"> <header android:title="intent"     android:summary="launches activity">     <intent android:action="com.gyebro.settingsintent.show_current_user" /> </header> </preference-headers> 

showcurrentuseractivity.java

public class showcurrentuseractivity extends activity { public alertdialog dialog = null; @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.dialog); } } 

and dialog.xml

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <textview     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="do want sign out?"     android:layout_margin="20dp"     android:id="@+id/textview" /> <linearlayout     style="?android:attr/buttonbarbuttonstyle"     android:orientation="horizontal"     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:weightsum="2">     <button         style="?android:attr/buttonbarbuttonstyle"         android:layout_width="0dp"         android:layout_weight="1"         android:layout_height="wrap_content"         android:text="sign out"         android:id="@+id/button1" />     <button         style="?android:attr/buttonbarbuttonstyle"         android:layout_width="0dp"         android:layout_weight="1"         android:layout_height="wrap_content"         android:text="cancel"         android:id="@+id/button2" /> </linearlayout> </linearlayout> 

e d t here's example, when have dialog fragment wrapped in activity. need finish activity after user interacts dialog. unless use dialog fragment @ other places too, recommend using above solution.

showcurrentuseractivity.java

public class showcurrentuseractivity extends activity {  private static final string tag = "showcurrentuseractivity"; @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     // show dialog     showcurrentuserdialog fragment = new showcurrentuserdialog();     fragment.show(getfragmentmanager(), "showcurrentuserdialog"); } public void dopositiveclick() {     // stuff here.     log.d(tag, "dialog positive click"); } public void donegativeclick() {     // stuff here.     log.d(tag, "dialog negative click"); } public void dialogdetached() {     log.d(tag, "dialog detached, finishing now...");     finish(); } } 

showcurrentuserdialog.java

public class showcurrentuserdialog extends dialogfragment {  @override public dialog oncreatedialog(bundle savedinstancestate) {     return new alertdialog.builder(getactivity())             .settitle("sign out")             .setmessage("do want sign out?")             .setpositivebutton("sign out",                     new dialoginterface.onclicklistener() {                         public void onclick(dialoginterface dialog, int whichbutton) {                             ((showcurrentuseractivity)getactivity()).dopositiveclick();                         }                     }             )             .setnegativebutton("cancel",                     new dialoginterface.onclicklistener() {                         public void onclick(dialoginterface dialog, int whichbutton) {                             ((showcurrentuseractivity)getactivity()).donegativeclick();                         }                     }             )             .create(); }  @override public void ondetach() {     super.ondetach();     ((showcurrentuseractivity)getactivity()).dialogdetached(); } } 

Comments

Popular posts from this blog

rdbms - what exactly the undo information lives in oracle? -

bash - How do you programmatically add a bats test? -

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -