1

The application works well in Android 2.3.5 But it does not work as desired in Nexus 4 (Android 4.2.2).

The application is: In the onCreate of main activity, it calls another activity through intent.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final Intent launchIntent = new Intent(MainActivity.this, AndroidVideoCapture.class);
    launchIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
    startActivity(launchIntent);

    // I add a button dynamically here
}

Now with Nexus 4, after it finishes the intent of AndroidVideoCapture, it returns back to the begining of the onCreate, "setContentView(R.layout.activity_main);". So the intent restarts again and again.

Why?

user1914692
  • 3,033
  • 5
  • 36
  • 61

2 Answers2

3

You can check if you have enabled 'Don't Keep Activities' in the developer options on the nexus 4 device. If checked, Disable that. This is an option available only since 4.0 and this is the reason behind your parent activity being 'recreated'.

siddharthsn
  • 1,709
  • 1
  • 21
  • 32
  • May I know why it is designed to be "recreated"? – user1914692 May 09 '13 at 00:09
  • This is purely a developer option that helps you verify if 1. You have Saved the state of the activity, before it goes background. 2. Handled Low Memory Situations properly(in which case the activity will be destroyed.) Now since it is destroyed, it is being recreated to give you the appearance that the Activity Stack is intact. – siddharthsn May 09 '13 at 06:54
2

Try adding

if (savedInstanceState == null) {
    final Intent launchIntent = new Intent(MainActivity.this, AndroidVideoCapture.class);
    launchIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
    startActivity(launchIntent);
}

savedInstanceState not being null means the activity is being recreated.

bclymer
  • 6,679
  • 2
  • 27
  • 36
  • Thank you! It works! But may I know what happened from Android 2.3.5 to 4.2.2, so that we need to add saveInstanceState? – user1914692 May 07 '13 at 19:42
  • It's likely something with tighter memory management. Android 4.2.2 uses more RAM on it's own, so there is less available to apps, and activities are suspended to free up RAM. – bclymer May 07 '13 at 20:18
  • Also, in general, Android makes no promises that your activity won't be destroyed anytime it isn't visible. So you really shouldn't assume it, even if it works sometimes (or most of the time) because it might behave differently on a different device, or if the user has different settings or apps in memory. – Jay Bobzin May 08 '13 at 01:45
  • But it is not only related to the children activity. My parent acitivity is beeing recreated. – user1914692 May 09 '13 at 00:09