且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

通知打开活动,按下后退按钮,主活动打开?

更新时间:2022-12-18 22:20:30

我从你的问题中了解到你的问题是在以下步骤中

Your problem is in following steps as I know from your question is

1.启动完成后创建通知

1.Create notification after boot complete

2.Main Activity 将在启动时调用

2.Main activity will call on start up

3.你按下home键,主要活动将停止但不会破坏

3.you pressed home button so main activity will be stopped but will not destroy

4.您单击状态栏中的通知,以便您的 应用程序将恢复,以便您的后台堆栈中已经有主要活动,通知将创建新活动,就像您在问题 NewNoteActivity 活动中提到的那样将推回堆栈.所以在这一步你有两个活动在后堆栈

4.You click on notification from status bar so your application will be resume so that you have already main activity in your back stack and notification will create new activity like you mentioned in your question NewNoteActivity activity will be push on back stack.SO at this step you have two activities in back stack

5.您按下后退按钮,以便最后一个活动将被销毁并且您的主要活动将被恢复但您想转到主页屏幕.

5.you pressed back button so that last activity will be destroy and your main activity will be resume But you want to go to homepage screen.

所以你的问题在于以下步骤,因为我从你的问题中知道

So the Your problem is in following steps as I know from your question is

1.启动完成后创建通知

1.Create notification after boot complete

2.Main Activity 将在启动时调用

2.Main activity will call on start up

3.你按下home键,主要活动将停止但不会破坏

3.you pressed home button so main activity will be stopped but will not destroy

4.您单击状态栏中的通知,以便您的 应用程序将恢复,以便您的后台堆栈中已经有主要活动,通知将创建新活动,就像您在问题 NewNoteActivity 活动中提到的那样将推回堆栈.所以在这一步你有两个活动在后堆栈

4.You click on notification from status bar so your application will be resume so that you have already main activity in your back stack and notification will create new activity like you mentioned in your question NewNoteActivity activity will be push on back stack.SO at this step you have two activities in back stack

5.您按下后退按钮以便最后一个活动将被销毁并且您的主要活动将恢复但是当您按下后退按钮时您想打开主页活动来自 NewNoteActivity 活动.

5.you pressed back button so that last activity will be destroy and your main activity will be resume But you want to open homepage activity when you pressed on back button from NewNoteActivity activity.

因此,解决方案是,当您从 NewNoteActivityactivity 中按下后退按钮时,您会使用 Intent.FLAG_ACTIVITY_CLEAR_TOP 标志再次启动主要活动,以便您的主要活动将重新创建并接收 onNewIntent() 方法,因此您可以获得标志并完成主要活动

So the solution is that when you pressed back button from your NewNoteActivityactivity you start main activity again with Intent.FLAG_ACTIVITY_CLEAR_TOP flag so that your main activity will recreate and will receive onNewIntent() method so there you can get flag and you can finish main activity

例如

@Override
    public void onBackPressed() {
        Intent i = new Intent(this, Main.class);
    i.putExtra("exit", true);
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(i);
    super.onBackPressed();
    }

现在你必须在你的主要活动中实现 onNewIntent() 方法

Now you have to implement onNewIntent() method in your Main activity

从 NewNoteActivity 活动按下后退按钮时,您的主要活动将调用 onNewIntent() 方法,因此在此方法中,您必须获取从 NewNoteActivity 活动传递的标志变量.如果您获得标志,如果它为真然后只需完成主要活动,以便您将获得主屏幕.

On back button pressed from NewNoteActivity activity your Main activity will call onNewIntent() method so in this method you have to fetch flag variable which is passed from NewNoteActivity activity.If you get flag and if it is true then just finish the Main activity so that you will get Home screen.

编辑

您是说您打开了 A、B 或 C 中的任何活动,然后按下后退按钮,此活动将关闭.如果当时堆栈中只有一个活动,您的应用程序将关闭意味着您将进入主屏幕.但是如果您有多个活动并且您按下后退按钮,您至少有一个活动在堆栈中,现在您单击通知,以便这将打开与您的通知相关联的新活动,以便此活动将被推到后堆栈上.现在,如果您按下后退按钮,如果您没有修改该活动中的 onBackPressed() 方法,则与您的通知关联的最后一个活动将关闭,然后如果有任何活动,它将检查返回堆栈在后台堆栈中,以便恢复活动,或者如果后台堆栈中没有活动,则您的应用程序将关闭,您将进入主屏幕

You are saying that you have any of the activity from A,B,or C is opened and you pressed back button so that this activity will be closed.If you have only one activity in stack at that time your application will be closed means you will get home screen.But if you have more than one activity and you pressed back button you have at least one activity in stack and now you click on notification so that this will open a new activity associated with your notification so that this activity will be pushed on that on back stack.Now if you pressed back button your last activity which is associated with your notification will be closed if you have not modify onBackPressed() method in that activity and then it will check back stack if any activity in back stack so that activity will be resumed or if there is no activity in back stack then your application will be closed and you will get home screen