使用android:action从设置启动活动

时间:2014-09-09 19:30:19

标签: android android-intent android-fragments

我已经定义了我的settings.xml文件,以包含在设置对话框中使用android:action项的项目。请参阅下面的该活动的示例代码。一切正常。然而,这件事是"覆盖"我的整个活动,当用户按下按钮时,整个应用程序完成。有没有办法推出一个" Fragment"在settings.xml中使用android:action或者我如何重构我的活动,以便在该活动完成后我的主要活动恢复?

<PreferenceScreen>
    <Preference android:title="Current User" >
        <intent android:action="com.example.coreui.ShowCurrentUserActivity"
        />
    </Preference>
</PreferenceScreen>

这是活动代码

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();
            }
        });
    }
}

这就是我在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>

1 个答案:

答案 0 :(得分:1)

如果对话框本身就是一个活动,它可以正常工作。此外,还可以创建仅显示片段的活动。 (我很快就会编辑我的答案。)

<强>的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 my 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);
}
}

最后是 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 you want to 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 I T 这是一个例子,当你有一个包含在Activity中的Dialog Fragment时。您需要在用户与Dialog交互后完成活动。除非您在其他地方使用Dialog Fragment,否则我建议您使用上述解决方案。

<强> ShowCurrentUserActivity.java

public class ShowCurrentUserActivity extends Activity {

private static final String TAG = "ShowCurrentUserActivity";
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Show the dialog
    ShowCurrentUserDialog fragment = new ShowCurrentUserDialog();
    fragment.show(getFragmentManager(), "ShowCurrentUserDialog");
}
public void doPositiveClick() {
    // Do stuff here.
    Log.d(TAG, "Dialog positive click");
}
public void doNegativeClick() {
    // Do 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 you want to 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();
}
}