活动生命周期的行为与JellyBean的行为不同

时间:2013-01-21 15:30:50

标签: android android-activity android-notifications android-lifecycle activity-lifecycle

我有以下情况。活动A启动活动B. B启动通知,单击启动另一个活动C.现在姜饼和ICS的行为是不同的。当我点击通知时看到姜饼,可以看到预期的行为,但是当我点击通知活动A时,我在ICS或JellyBean上运行相同的代码被销毁(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");
    }
}

1 个答案:

答案 0 :(得分:0)

系统可以在需要时始终终止活动。可能它不是Android版本之间的差异,而是不同设备之间的差异/不同Android版本的可用内存量不等。

相关问题