通知打开活动,按下后退按钮,主活动打开? [英] Notification opens activity, back button pressed, main activity is opened?

查看:23
本文介绍了通知打开活动,按下后退按钮,主活动打开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能描述我的问题的最好方式是这样的:

The best way I can describe my problem is like this:

  1. 在启动时创建通知(使用 BroadcastReceiver).
  2. 我的应用主 Activity 已打开并按下主页按钮(应用仍在后台运行,直到系统将其关闭).
  3. 我拉下状态栏并按下之前在启动时创建的通知.
  4. 一些与主要活动不同的活动已启动.
  5. 我按下后退按钮,显示主要活动.

我怎样才能阻止最后一步?我想要使​​用后退按钮返回我所在的位置,即主屏幕(带有所有小部件和应用程序图标的桌面).我的应用的主要 Activity 应该在后台运行,为什么用后退按钮调用它?

How can I prevent that last step? What I want with the back button is to go back where I was, which is the home screen (the desktop with all the widgets and app icons). My app's main activity was supposed to be running on the background, why was it called with the back button?

如果相关,我创建通知的代码如下:

In case it's relevant, my code to create a notification goes like this:

public void createNotification(int notifyId, int iconId, String contentTitle, String contentText) {
    Intent intent = new Intent(mContext, NewNoteActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.putExtra(AgendaNotesAdapter.KEY_ROW_ID, (long)notifyId);

    PendingIntent contentIntent = PendingIntent.getActivity(mContext, notifyId, intent, 0);

    Notification notification = new Notification(iconId, contentTitle, 0);
    notification.setLatestEventInfo(mContext, contentTitle, contentText, contentIntent);

    mNotificationManager.notify(notifyId, notification);

我尝试向 intent 添加更多标志组合,但它们都没有解决我的问题...建议?

I tried to add a couple of more flags combinations to intent but neither of them solved my problem... Suggestions?

推荐答案

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

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

这篇关于通知打开活动,按下后退按钮,主活动打开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆