0

I have two Activities in my app:

A - login Activity

B - main Activity

When the user clicks the login button in activity A I am starting a service. In the service's onCreate() method I launch activity B like this:

 Intent notificationIntent = new Intent(this, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

    NotificationCompat.Builder builder = new NotificationCompat
            .Builder(getApplicationContext());
    builder.setContentIntent(pendingIntent);
    builder.setContentTitle("Activity B in foreground service");
    builder.setSmallIcon(R.drawable.ic_launcher);

    startForeground(1, builder.build());

The problem is that Activity B launches minimized. I have to press the service button in the notification bar in order to get my activity maximized (fill the screen). How could I launch Activity B from service in normal way - without minimizing it (putting to background)?

barq
  • 3,681
  • 4
  • 26
  • 39
user2999943
  • 2,419
  • 3
  • 22
  • 34

3 Answers3

1

Your code doesn't actually launch Activity B at all. All it does is create a Notification, that, if selected by the user, will launch Activity B.

If you want to actually launch Activity B, do this:

    Intent notificationIntent = new Intent(this, MainActivity.class);
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    // Now launch the activity immediately
    startActivity(notificationIntent);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    NotificationCompat.Builder builder = new NotificationCompat
        .Builder(getApplicationContext());
    ... (rest of your code here)
David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • Your solution works but when I press on service Notifcation later - Activity B reloads to initial state. For example, if I open fragment in Activity B and press service Notifcation button - Activity B reloads without opened fragment. This behaviour I didn't noticed in my initial code sample. Maybe there is a way to open service Notification programmatically? – user2999943 Feb 02 '15 at 14:57
  • 1
    You can fix that by adding `Intent.FLAG_ACTIVITY_SINGLE_TOP` to `notificationIntent` or by declaring Activity B with `launchMode="singleTop"` in the manifest. This will prevent Android from creating a new instance of Activity B if there is already an instance of Activity B on the top of the activity stack in your task. – David Wasser Feb 02 '15 at 16:33
0

Question already answered in android start activity from service

public void sendMessage(View view) {
    Intent intent = new Intent(this, DisplayMessageActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
}

http://developer.android.com/training/basics/firstapp/starting-activity.html

Community
  • 1
  • 1
Cativail
  • 1,040
  • 1
  • 9
  • 18
0

Finally I found out the solution:

onCreate() method in service class:

@Override
public void onCreate() {

    Intent notificationIntent = new Intent(getBaseContext(), MainActivity.class);
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    getApplication().startActivity(notificationIntent);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

    NotificationCompat.Builder builder = new NotificationCompat
            .Builder(getApplicationContext());
    builder.setContentIntent(pendingIntent);
    builder.setContentTitle("Activity B runs in a foreground service");
    builder.setSmallIcon(R.drawable.ic_launcher);

    startForeground(1, builder.build());

}

In AndroidManifest.xml add this line in Activity element:

android:launchMode="singleTop"
user2999943
  • 2,419
  • 3
  • 22
  • 34