2014-10-22 4 views
0

Я разрабатываю приложение для Android и пытаюсь реализовать навигацию вверх как на панели действий, так и на кнопке обратной связи по умолчанию. Мне нужно заставить его вернуться к предыдущему действию, но это просто закрытие приложения.Вверх приложение для перехода на Android-приложение

Здесь я прочитал руководство по проектированию http://developer.android.com/design/patterns/navigation.html и материал для внедрения здесь http://developer.android.com/training/implementing-navigation/ancestral.html, но у меня все еще есть проблемы.

Вот мой код:

Мои 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> 

Это показывает, что страница профиля должна вернуться к основному виду деятельности, но вместо этого закрывает приложение.

Я предполагаю, что это может быть потому, что я собираюсь «через» диалог фрагмента, но не знаю, как его исправить.

ответ

0

Наконец, после веков копаться в коде, я обнаружил, что причина выходов приложения была вызвана одной строкой в ​​диалоговых фрагментах:

context.finish(); 
Смежные вопросы