问题
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");
}
}
回答1:
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.
来源:https://stackoverflow.com/questions/14441951/activity-lifecycle-behaves-differently-jellybean-onwards