0

I have the following scenario. Activity A starts Activity B. B starts a Notification clicking on which another activity C is started. Now the behaviour is different on Gingerbread and ICS onwards. Incase of Gingerbread when I click on the Notification, the expected behavior is seen, however when I run the same code on ICS or JellyBean when I click on the Notification Activity A is destroyed(OnDestroy is called). Why is the lifecycle behavior different. How can I make it to behave in a consistent way in all devices? Please suggest.

    public class MainActivity extends Activity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Log.v("MyLog","Activity A created");
        }

        @Override
        protected void onDestroy() {
            super.onDestroy();
            Log.v("MyLog","Activity A destroyed");

        }
        public void startB(View v)
        {
            Intent intent=new Intent(getApplicationContext(),B.class);
            startActivity(intent);
        }

        protected void onStop()
        {
            super.onStop();
            Log.v("MyLog","Activity A stopped");
        }

        protected void onResume()
        {
            super.onResume();
            Log.v("MyLog","Activity A resumed");
        }


    }
}

public class B extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.b);
        Log.v("MyLog","Activity B created");
    }

    protected void onStop()
    {
        super.onStop();
        Log.v("MyLog","Activity B stopped");
    }

    protected void onResume()
    {
        super.onResume();
        Log.v("MyLog","Activity B resumed");
    }

    public void startNotification(View v)
    {
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(getApplicationContext())
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("My notification")
        .setContentText("Hello World!")
        .setAutoCancel(true);



        // Creates an explicit intent for an Activity in your app
        Intent resultIntent = new Intent(getApplicationContext(), C.class);

        // The stack builder object will contain an artificial back stack for the
        // started Activity.
        // This ensures that navigating backward from the Activity leads out of
        // your application to the Home screen.
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(getApplicationContext());
        // Adds the back stack for the Intent (but not the Intent itself)
        stackBuilder.addParentStack(C.class);
        // Adds the Intent that starts the Activity to the top of the stack
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);

        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        // mId allows you to update the notification later on.
        int mId=1;
        mNotificationManager.notify(mId, mBuilder.build());
    }
}

public class C extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.c);
        Log.v("MyLog","Activity C created");
    }
    protected void onStop()
    {
        super.onStop();
        Log.v("MyLog","Activity C stopped");
    }

    protected void onResume()
    {
        super.onResume();
        Log.v("MyLog","Activity C resumed");
    }
}
Ricky
  • 81
  • 11

1 Answers1

0

System can terminate activity always when it's needed. Probably it's not exectly difference between Android releases, but differece between different devices / free memory amount of different Android versions runing.

piotrpo
  • 12,398
  • 7
  • 42
  • 58
  • Yes, I agree, but then I can expect it to behave differently sometimes atleast, but I get the exact same result everytime, I have verified the same with two versions of emulators too, one created with android 2.3.3 and the other with 4.1.2. The Activity A is destroyed each in the 4.1.2 device/emulator. There is no mention of this behavior on the Developers Guide. That is what puzzles me. – Ricky Jan 21 '13 at 20:06
  • You may verify the same with the code that I have posted, please let me know if you observe the same thing. – Ricky Jan 21 '13 at 20:07
  • I don't know "why" - sorry. Docs say, that everything in background can be killed at any moment, including foreground services. Don't you use some big bitmaps in your layout, or some other large memory objects? – piotrpo Jan 22 '13 at 08:34
  • Nope nothing like that, just a simple TextView and a Button, Yes I am aware the docs say it "can" be killed but the case I am referring to is more of a "will" be killed as it is happening every single time. Its consistent behavior – Ricky Jan 22 '13 at 09:02