简体   繁体   English

Android意向-未恢复活动

[英]Android Intent - Activity not Resumed

I'm having trouble resuming an activity. 我无法恢复活动。

Here's what happening: 这是发生了什么:

Activity A calls activity B : 活动A调用活动B:

Intent intent = new Intent(A.this, B.class);
startActivity(intent);

Then Activity B Sets a Notification and calls Activity A: 然后活动B设置通知并调用活动A:

Intent intent = new Intent(this, MrpCastPlayerActivity.class);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
        intent, 0);

    android.support.v4.app.NotificationCompat.Builder notif = new NotificationCompat.Builder(this);
    notif.setContentIntent(pendingIntent)
            .setWhen(System.currentTimeMillis())
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("TITLE")
            .setContentText("CONTENT")
            .setAutoCancel(true);
    notif.setOngoing(true);
  Notification notification = notif.build();
  NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

      notificationmanager.notify(0, notification);
      Intent intent2 = new Intent(B.this, A.class);
      startActivity(intent2);

The problem is when I tap the notification it opens Activity B but it does't call the onResume it calls onCreate . 问题是当我点击通知时,它打开了活动B,但没有调用onResume而是调用了onCreate If I use moveTaskToBack(true) , instead of starting intent2 , onResume is called. 如果我使用moveTaskToBack(true) ,而不是启动intent2 ,则会调用onResume It seems that Activity B is finished when I use intent2 to open Activity A . 当我使用intent2打开Activity A时,似乎活动B已完成。
Is there a away to avoid this ? 有没有办法避免这种情况?

To resume your activity, you should considering adding flags to your Intent . 要恢复活动,您应该考虑将标志添加到Intent

do nothing inside the method onResume() and onstart() when coming back to this activity, Try this 回到此活动时,在方法onResume()和onstart()内什么也不做,请尝试

 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

Manifest Changes: 清单变更:

<activity android:name=".MyActivity"

              android:configChanges="keyboardHidden|orientation">

Hope this helps 希望这可以帮助

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

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