Android提供导航问题

时间:2015-11-11 11:09:36

标签: android android-notifications android-pendingintent

我想创建正在进行适当活动的应用。我已经创建了通知。

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(MyService.this)
    .setContentTitle(getResources().getText(R.string.app_name))
    .setContentText(message)
    .setSmallIcon(icon)
    .setWhen(System.currentTimeMillis())
    .setDefaults(Notification.DEFAULT_ALL);
Intent intent = ErrorStateActivity_.intent(MyService.this)
    .flags(Intent.FLAG_ACTIVITY_NEW_TASK |
            Intent.FLAG_ACTIVITY_CLEAR_TASK)
    .errorCodeExtra(ErrorStateActivity.NO_INTERNET_CONNECTION_CODE)
    .get();
PendingIntent contentIntent = PendingIntent.getActivity(
    MyService.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.setContentIntent(contentIntent);
Notification notification = notificationBuilder.build();
notification.defaults = Notification.DEFAULT_ALL;
notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE;
mNotificationManager.notify(NotificationConstants.INTERNET_CONNECTION_ERROR, notification);

这是清单

<activity
   android:name=".activity.MainActivity_"
   android:configChanges="orientation|screenSize"
   android:launchMode="singleTop">
</activity>
<activity
   android:name=".activity.ErrorStateActivity_"
   android:configChanges="orientation|screenSize"
   android:parentActivityName=".activity.MainActivity_">
   <meta-data
       android:name="android.support.PARENT_ACTIVITY"
       android:value=".activity.MainActivity_"/>
</activity>

这是My ErrorStateActivity部分

@Override
public boolean onOptionsItemSelected(MenuItem item) {
   switch (item.getItemId()) {
    case android.R.id.home: {
        Intent upIntent = NavUtils.getParentActivityIntent(this);
        if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
            // This activity is NOT part of this app's task, so create a new task
            // when navigating up, with a synthesized back stack.
            TaskStackBuilder.create(this)
                    // Add all of this activity's parents to the back stack
                    .addNextIntentWithParentStack(upIntent)
                            // Navigate up to the closest parent
                    .startActivities();
        } else {
            // This activity is part of this app's task, so simply
            // navigate up to the logical parent activity.
            NavUtils.navigateUpTo(this, upIntent);
        }
        return true;
       }
    default:
        return super.onOptionsItemSelected(item);
    }

}

当通知出现并点击它时,ErrorStateActivity会打开,然后点击返回按钮它会支持MainActivity。没问题。!!!!但是,当应用程序从最近的应用程序中杀死应用程序并执行上面的过程时,它不会回到MainActivity。请帮我!提前谢谢。

1 个答案:

答案 0 :(得分:0)

@sam_k尝试测试

@Override
public void onBackPressed() {
    handleBack();
}

private void handleBack() {
    Intent upIntent = NavUtils.getParentActivityIntent(this);
    if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
        // This activity is NOT part of this app's task, so create a new task
        // when navigating up, with a synthesized back stack.
        TaskStackBuilder.create(this)
                // Add all of this activity's parents to the back stack
                .addNextIntentWithParentStack(upIntent)
                        // Navigate up to the closest parent
                .startActivities();
    } else {
        // This activity is part of this app's task, so simply
        // navigate up to the logical parent activity.
        NavUtils.navigateUpTo(this, upIntent);
    }
    ErrorStateActivity.this.finish();
}