如何从通知活动返回主活动?

时间:2018-08-07 15:46:25

标签: java android

当我按下通知时,活动将打开,但是当我单击返回时,应用程序将回到后台,并且仍在同一活动中。 当我单击返回时,我想返回到MainActivity。我的代码有什么问题?

public class MainActivity extends AppCompatActivity {

    NotificationManagerCompat notificationManager;
    NotificationCompat.Builder n;
    String ID = "a" ;
    int id=1 ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

    }


    public void b2(View view) {

       Intent i = new Intent(MainActivity.this, Main2.class);

       TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
       stackBuilder.addParentStack(MainActivity.class);
       stackBuilder.addNextIntent(i);


       PendingIntent pi =  stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);

       n = new NotificationCompat.Builder(this, ID)
            .setSmallIcon(R.drawable.xx)
            .setContentTitle("don't forget")
            .setContentText("Hello0o0o0o")
            .setContentIntent(pi)
            .setAutoCancel(true);

       notificationManager = NotificationManagerCompat.from(this);
       notificationManager.notify(id , n.build());
       id++;
   }



   public void b1(View view) {
       Intent i4 = new Intent(this,Read.class);
       startActivity(i4);
   }
}

1 个答案:

答案 0 :(得分:0)

从您的代码中看不到问题,但是您可以重写onBackPressed()函数以移回MainActivity。这是一个解决方案。还要检查您是否实际上在调用此函数b2。