5

I have developed an app in android 4.0.3(Ice-cream Sandwich), i am using two activities to test the activity navigation.But i Observed a different behavior in Activity navigation.

I am calling Activity B From Activity A. In Activity B i am just calling the finish() method. So that we can see the Previous Activity A. It is exactly working as expected but the problem is for back navigation(Calling finish method or pressing back-key),it is calling onCreate() Method of Activity A instead of calling the onResume(). But in previous versions it is not behaving like this. Is this a new Implementation in android 4.0??

Here is the example which i implemented:

Activity_A:

public class Activity_A extends Activity {

    /** Called when the activity is first created. */
    static int count=0;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView text=(TextView)findViewById(R.id.textcontent);
        text.setText("Activity 1 called:"+(++count)+" Times");
    }

    public void onClick(View v)
    {
        Intent intent=new Intent(this,Activity2.class);
        startActivityForResult(intent, 1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.d("onActivityResult", "Called with Code:"+resultCode);
    }

}

Activity_B:

public class Activity_B extends Activity {

      /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView text=(TextView)findViewById(R.id.textcontent);
        text.setText("Activity 2");
    }

    public void onClick(View v)
    {
        setResult(1);
        finish();
    }
}

Please Check and let me know if i am doing any mistake.

Thanks, Ram.

WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
ram
  • 3,487
  • 10
  • 33
  • 47
  • Question: Does onResume come after onCreate? I think Activity A was dropped by Android after calling Activity B - something that might happen on all Android platforms I guess. – Harald Wilhelm Dec 23 '11 at 14:25

4 Answers4

7

I have the same problem!! Go to Settings/Development/ and uncheck 'Destroy Activities'

lorbios
  • 71
  • 1
  • **Note** :- Phone must get restarted in order to see the effect. – Deepak May 09 '12 at 13:51
  • if i open camera to capture image on android 4.03 it called onActivityResult method before i capture or cancel image, so the resultcode always be null and i couldn't handle image capture event on onActivityResult. do have any idea why this happen? i am testing this on htc me 4.03, although this works on i-tab intext 4.04 version. – Hiren Dabhi Sep 25 '12 at 09:21
1

This is from the Android Activity documentation (Link here):

  • If an activity in the foreground of the screen (at the top of the stack), it is active or running.
  • If an activity has lost focus but is still visible (that is, a new non-full-sized or transparent activity has focus on top of your activity), it is paused. A paused activity is completely alive (it maintains all state and member information and remains attached to the window manager), but can be killed by the system in extreme low memory situations.
  • If an activity is completely obscured by another activity, it is stopped. It still retains all state and member information, however, it is no longer visible to the user so its window is hidden and it will often be killed by the system when memory is needed elsewhere.
  • If an activity is paused or stopped, the system can drop the activity from memory by either asking it to finish, or simply killing its process. When it is displayed again to the user, it must be completely restarted and restored to its previous state.

Perhaps points 3 and 4 are relevant to you.

Davos555
  • 1,974
  • 15
  • 23
  • but it is just about two activities.And more over it is the first 3rd party app in my device(Samsung Nexus S) after updating it to 4.0.3, Then how it could be Low Memory Issue?? – ram Dec 23 '11 at 14:43
  • From the activity diagram - if your activity obscures the old activity, then the activity is stopped. If the system doesnt need memory it calls onRestart, rather than onResume. Maybe this is where your program is starting from? They haven't mentioned any changes in icecream regarding going to onCreate from onStop, unless the system needs memory. – Davos555 Dec 23 '11 at 14:51
0

See this answer: https://stackoverflow.com/a/16147110/1306419 . I quote from there:

You might need to declare the launch mode of your activity A(parent activity) as: android:launchMode="singleTop" in your AndroidManifest.xml. if you don't do that, then Android uses standard launch mode, which means The system always creates a new instance of the activity in the target task. and the activity is recreated (Android documentation).

With singleTop the system returns to your existing activity (with the original extra), if it is on the top of the back stack of the task.

Community
  • 1
  • 1
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
0

And to maintain the state of the activity use onSavedInstanceState() and onRestoreInstanceSate() methods.

ram
  • 3,487
  • 10
  • 33
  • 47