2013-05-02 2 views
0

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

Проблема в том, что сейчас создается новый экземпляр, и все прежние данные теряются. Что я делаю не так?

NotificationManager nm = (NotificationManager) getSystemService("notification"); 
Intent intent = new Intent(this, beerwarn.class); 
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); 
stackBuilder.addParentStack(beerwarn.class); 
stackBuilder.addNextIntent(intent); 
PendingIntent pIntent = stackBuilder.getPendingIntent 
              (0, PendingIntent.FLAG_UPDATE_CURRENT); 
NotificationCompat.Builder builder = new NotificationCompat.Builder(this); 
builder.setContentIntent(pIntent) 
     //Notification noti = new Notification.Builder(this) 
     .setContentTitle("BAC Level Notice") 
     .setContentText("Your BAC has dropped below Max Legal BAC") 
     .setSmallIcon(R.drawable.ic_launcher) 
     .setContentIntent(pIntent) 
     .setSmallIcon(R.drawable.beerwarn); 
     //builder.flags |= Notification.FLAG_AUTO_CANCEL; 

nm.notify(1, builder.build()); 

ответ

0

Добавить android: launchMode = singleTask или SingleInstance в вашей деятельности в AndroidManifest.xml

<activity 
     android:name="yourmainscreenActivity" 
     android:exported="false" 
     android:launchMode="singleTask" > 
     <intent-filter> 
      <action android:name="android.intent.action.VIEW" /> 

      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
    </activity> 
0

Вот мое предложение, когда Вы поднимаете экран сообщение, четко и любой другой деятельности.

NotificationManager nm = (NotificationManager) getSystemService("notification"); 
Intent intent = new Intent(this, beerwarn.class); 
intent .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
intent .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); 
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent , PendingIntent.FLAG_UPDATE_CURRENT); 
NotificationCompat.Builder builder = new NotificationCompat.Builder(this); 
builder.setContentIntent(pIntent) 
    .setContentTitle("BAC Level Notice") 
    .setContentText("Your BAC has dropped below Max Legal BAC") 
    .setSmallIcon(R.drawable.ic_launcher) 
    .setContentIntent(pIntent) 
    .setSmallIcon(R.drawable.beerwarn); 

nm.notify(1, builder.build()); 

На вашем beerwarn.class, overrride в onBackPressed функции, проверьте, работает ли ваша деятельность В начало, предполагая, что оно названо MainScreen.class, если нет, то запустить его.

ActivityManager mngr = (ActivityManager) getSystemService(ACTIVITY_SERVICE); 
List<ActivityManager.RunningTaskInfo> taskList = mngr.getRunningTasks(10); 
for (ActivityManager.RunningTaskInfo rti : taskList) { 
    if (rti.topActivity.getClassName().equals(this.getClass().getName())) { 
     if (!rti.baseActivity.getClassName().equals(MainScreen.class.getName())) { 
      Intent i = new Intent(this, MainScreen.class); 
      i.putExtra("currentTab", getIntent().getIntExtra("currentTab", 1)); 
      startActivity(i); 
      break; 
     } 
    } 
} 

super.onBackPressed(); 
Смежные вопросы