ram
ram

Reputation: 3507

Is this a New Implementation in Android 4.0?

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.

Upvotes: 5

Views: 999

Answers (4)

Shobhit Puri
Shobhit Puri

Reputation: 26017

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.

Upvotes: 0

ram
ram

Reputation: 3507

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

Upvotes: 0

lorbios
lorbios

Reputation: 71

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

Upvotes: 7

Davos555
Davos555

Reputation: 2004

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.

Upvotes: 1

Related Questions