我有2个活动A和B. A开始B(对于清单中的B,我将标志noHistory设置为true)。在Activity B上有单个Button启动DialogFragment(不添加到backStack)。 当对话框启动时,我按下设备上的后退按钮,我看到活动B,但我想看活动A. 暂时我在我的Dialog的onCancel()中调用getActivity.finish()解决了这个问题。它有效,但我会对更好的解决方案感到满意。 我很感激你的想法。
更新:代码
这是ActivityA:
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);
}
}
这是Dialog
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>