开发者

Notification opens activity, back button pressed, main activity is opened?

开发者 https://www.devze.com 2023-04-09 23:49 出处:网络
The best way I can describe my problem is like this: A notification is created at boot (with a BroadcastReceiver).

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

  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.

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, contentT开发者_如何学Pythonitle, 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?


For whose who still might need answer. It looks like this is what you want to achieve:

When you start an Activity from a notification, you must preserve the user's expected navigation experience. Clicking Back should take the user back through the application's normal work flow to the Home screen, and clicking Recents should show the Activity as a separate task.

http://developer.android.com/guide/topics/ui/notifiers/notifications.html#NotificationResponse

Your situation is - Setting up a regular activity PendingIntent

See full steps in the link. Basically you need to:
1. Define Activity hierarchy in AndroidManifest.xml

<activity
    android:name=".MainActivity"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<activity
    android:name=".ResultActivity"
    android:parentActivityName=".MainActivity">
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value=".MainActivity"/>
</activity>


2. Create a back stack based on the Intent that starts the Activity:

...
Intent resultIntent = new Intent(this, ResultActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack
stackBuilder.addParentStack(ResultActivity.class);
// Adds the Intent to the top of the stack
stackBuilder.addNextIntent(resultIntent);
// Gets a PendingIntent containing the entire back stack
PendingIntent resultPendingIntent =
        stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
...
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(id, builder.build());


Had the same problem. What worked for me was setting in the manifest the following attribute to the activity launched by the notification:

android:taskAffinity=""

This essentially separates the affinity of the notification activity from the main activity and so the OS doesn't feel the need to "return" to the main activity, but simply returns to whatever was there before e.g. the "desktop".


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

1.Create notification after boot complete

2.Main activity will call on start up

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

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.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.Create notification after boot complete

2.Main activity will call on start up

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

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.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.

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

for example

@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();
    }

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

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.

EDIT

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


I use this solution in my apps. Override onBackPressed (in your activity routed from notification) and check if your app is in stack or not. if not then start your root activity. User can always navigate from your root activity. This is what I do in my apps

@Override
public void onBackPressed() {
    if (isTaskRoot()) {
        Intent intent = new Intent(this,YourMainActivity.class);
        startActivity(intent);
        super.onBackPressed();
    }else {
        super.onBackPressed();
    }
}


It works for me. try this:

notifyIntent.setAction(Intent.ACTION_MAIN);
notifyIntent.addCategory(Intent.CATEGORY_LAUNCHER);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号