Java Android开发中的Up导航退出应用

Java Android开发中的Up导航退出应用,java,android,eclipse,android-activity,android-fragments,Java,Android,Eclipse,Android Activity,Android Fragments,我正在开发一个Android应用程序,并尝试在操作栏按钮和默认的Android后退按钮上实现向上导航。我需要让它返回到以前的活动,但它只是关闭应用程序 我已经阅读了这里的设计指南 还有这里的实现工作,但我仍然有问题 这是我的密码: My MapActivity调用一个对话框片段: public void showProfileDialog() { // Create an instance of the dialog fragment and show it Profil

我正在开发一个Android应用程序,并尝试在操作栏按钮和默认的Android后退按钮上实现向上导航。我需要让它返回到以前的活动,但它只是关闭应用程序

我已经阅读了这里的设计指南 还有这里的实现工作,但我仍然有问题

这是我的密码:

My MapActivity调用一个对话框片段:

    public void showProfileDialog() {
    // Create an instance of the dialog fragment and show it
    ProfileDialogFragment profileDialog = new ProfileDialogFragment();
    profileDialog.show(getSupportFragmentManager(), "ProfileDialogFragment");
}
这是:

public class ProfileDialogFragment extends DialogFragment {

        protected FragmentActivity context;

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            LayoutInflater inflater = getActivity().getLayoutInflater();
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder.setView(inflater.inflate(R.layout.dialog_profile, null));
            builder.setMessage(R.string.profileName)
            .setPositiveButton(R.string.profileButtonTextEdit, new DialogInterface.OnClickListener() {
                   @Override
                   public void onClick(DialogInterface dialog, int id) {

                       //go to profile page
                       context = getActivity();
                       Intent i = new Intent(context,ProfilePageActivity.class);
                       i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                       //i.putExtra("key", "value"); //Optional parameters
                       context.startActivity(i);
                       context.finish();

                   }
               })
               .setNegativeButton(R.string.profileButtonTextClose, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       //effectivly a cancel button
                       ProfileDialogFragment.this.getDialog().cancel();
                   }
               }); 
            // Create the AlertDialog object and return it
            return builder.create();
        }
}
这将调用配置文件页面:

public class ProfilePageActivity extends FragmentActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.profile_screen);
        getActionBar().setDisplayHomeAsUpEnabled(true); // make up navigation

        final Button btnSave = (Button) findViewById(R.id.btnSave);
        btnSave.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Perform action on click
            }
        });

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
        }
        return false;
    }
}
在清单中,我指定了父活动:

  <activity 
        android:name="com.wc.test.ProfilePageActivity"
        android:label="@string/app_name"
        android:parentActivityName="com.wc.test.MyMapActivity">
        <!-- The meta-data element is needed for versions lower than 4.1 -->
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="com.wc.test.MyMapActivity" />

    </activity>

这表明配置文件页面应该返回到主活动,但会关闭应用程序


我想这可能是因为我正在“通过”一个片段对话框,但不确定如何修复它。

经过多年的代码挖掘,我终于发现应用程序退出的原因是由对话框片段中的一行引起的:

context.finish();