我有 2 个活动 A 和 B。A 开始 B(对于清单中的 B,我将标志 noHistory 设置为 true)。在 Activity B 上,有一个 Button 启动 DialogFragment(不添加到 backStack)。当对话框启动时,我按下设备上的后退按钮,我看到 Activity B,但我想看到 Activity A。暂时我解决了在我的 Dialog 的 onCancel() 中调用 getActivity.finish() 的问题。它有效,但我会对更好的解决方案更满意。我会很感激你的想法。
更新:代码
这是活动A:
public class Activity1 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button btn1 = new Button(getApplicationContext());
btn1.setText("Start Activity2");
btn1.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), Activity2.class);
startActivity(intent);
}
});
setContentView(btn1);
}
}
(这是活动B)
public class Activity2 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout ll = new LinearLayout(getApplicationContext());
TextView tv = new TextView(getApplicationContext());
tv.setText("This is Activity 2");
Button btn = new Button(getApplicationContext());
btn.setText("ShowDialog");
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
MyDialog dialog = new MyDialog();
dialog.show(fragmentTransaction, "dialog");
}
});
ll.addView(tv);
ll.addView(btn);
setContentView(ll);
}
}
这是对话框
public class MyDialog extends DialogFragment {
Activity2 activity = null;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("SomeMessage")
.setPositiveButton("Fire", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// FIRE ZE MISSILES!
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
return builder.create();
}
@Override
public void onCancel(DialogInterface dialog) {
super.onCancel(dialog);
if(activity != null)
activity.finish();
// getActivity().onBackPressed();
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if(activity instanceof Activity2) {
this.activity = (Activity2)activity;
}
}
}
这很明显
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.task_test"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="11"/>
<application android:label="@string/app_name">
<activity android:name="MyActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".Activity2"
android:theme="@android:style/Animation.Dialog"
android:noHistory="true"/>
<activity android:name=".Activity3"/>
</application>
</manifest>