0

我有以下情况。活动 A 启动活动 B。B 启动一个通知,单击该通知将启动另一个活动 C。现在 Gingerbread 和 ICS 以后的行为有所不同。当我单击通知时,如果是 Gingerbread,则会看到预期的行为,但是当我在单击通知时在 ICS 或 JellyBean 上运行相同的代码时,活动 A 被破坏(调用 OnDestroy)。为什么生命周期行为不同。如何使其在所有设备中以一致的方式运行?请建议。

    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");
    }
}
4

1 回答 1

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.

于 2013-01-21T15:49:44.647 回答