通知打开活动,返回键pressed,主要业务是开的呢? [英] Notification opens activity, back button pressed, main activity is opened?

查看:146
本文介绍了通知打开活动,返回键pressed,主要业务是开的呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以描述我的问题,最好的办法是这样的:

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

  1. 的通知在引导创建(用的BroadcastReceiver )。
  2. 在我的应用程序主要活动被打开,home键是pressed(应用程序在后台仍在运行,直到系统关闭的话)。
  3. 在我拉下在启动pviously创建的通知$ P $状态栏和preSS。
  4. 在一些活动中,从主要的一个不同的,被启动。
  5. 在我显示preSS后退按钮和主要活动。
  1. A notification is created at boot (with a BroadcastReceiver).
  2. My app main activity is opened and the home button is pressed (the app is still running in the background until the system closes it).
  3. I pull down the status bar and press on the notification previously created at boot.
  4. Some activity, different from the main one, is started.
  5. I press the back button and the main activity is displayed.

我怎么能prevent这最后一步?我想和后退按钮是回到我在那里,这是主屏幕(桌面所有的部件和应用程序图标)。我的应用程序的主要活动本来是要在后台,为什么是它与后退按钮叫跑?

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?

在情况下,它是相关的,我的code创建一个通知是这样的:

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);

我试着一对夫妇的多个标志的组合添加到意图但都没有解决我的问题......建议?

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 will call on start up

3。你pressed home键,以便主要活动将被停止,但不会破坏

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

4.您从状态栏点击通知,所以你的应用程序将被恢复让你在你的背部栈已经通知主要活动将创建像你这样的新活动在你的问题NewNoteActivity活动中提到将在逼退stack.SO在这一步你在后面的堆栈两项活动

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.您pressed后退按钮使最后一项活动将破坏和您的主要活动将恢复但是你想去的主页屏幕。

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 will call on start up

3。你pressed home键,以便主要活动将被停止,但不会破坏

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

4.您从状态栏点击通知,所以你的应用程序将被恢复让你在你的背部栈已经通知主要活动将创建像你这样的新活动在你的问题NewNoteActivity活动中提到将在逼退stack.SO在这一步你在后面的堆栈两项活动

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.您pressed后退按钮使最后一项活动将破坏和您的主要活动将恢复但是你要打开的网页活动时,你起价NewNoteActivity活动pssed背面按钮p $。

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.

所以,解决的办法是,当你pressed后退按钮从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()方法pssed所以在这种方法,你要取它是由NewNoteActivity传递标志变量返回按钮$ P $ activity.If你的标志,如果是真,那么刚刚完成的主要活动,这样你会得到主屏幕。

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被打开,你pressed后退按钮,使本次活动将是closed.If你在堆栈中只有一个活动,当时活动的您应用程序将被关闭意味着你会回家screen.But,如果你有一个以上的活动,你pressed后退按钮,你有一叠至少一个活动,现在你点击的通知,使这将打开关联的新活动与您的通知使这一活动将推动上背部stack.Now如果pressed后退按钮你这是与你有关通知最后一次活动将被关闭,如果你还没有修改onBack pressed()方法在该活动,然后它会回来检查堆栈如果回堆栈的任何活动,这样的活动将被收回,或者如果在后面的堆栈没有活动则应用程序将被关闭,你会得到主屏幕

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

这篇关于通知打开活动,返回键pressed,主要业务是开的呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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