简体   繁体   English

Android开发中的向上导航退出应用

[英]Up navigation quitting app in Android development

I'm developing an Android app, and am trying to implement the up navigation on both the action bar button and the default android back button. 我正在开发一个Android应用,并尝试在操作栏按钮和默认的android后退按钮上实现向上导航。 I need to make it return to the previous activity, however it's just closing the app. 我需要使其返回上一个活动,但是它只是关闭了该应用程序。

I've read the design guide here http://developer.android.com/design/patterns/navigation.html and the implementation stuff here http://developer.android.com/training/implementing-navigation/ancestral.html but am still having trouble. 我读过的设计指南这里http://developer.android.com/design/patterns/navigation.html和实现东西在这里http://developer.android.com/training/implementing-navigation/ancestral.html但仍然有麻烦。

Here is my code: 这是我的代码:

My MapActivity calls a dialog fragment: 我的MapActivity调用一个对话框片段:

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

which is here: 在这里:

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

and this calls the profile page: 这将调用个人资料页面:

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

and in the manifest, i specify the parent activity: 在清单中,我指定父活动:

  <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>

This shows that the profile page should go back to the main activity, but instead closes the app. 这表明配置文件页面应返回到主要活动,而是关闭应用程序。

I guess this could be because I'm going "via" a fragment dialog, but am not sure how to fix it. 我想这可能是因为我要“通过”一个片段对话框,但是不确定如何解决它。

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

context.finish();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM