Reputation: 3507
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
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
Reputation: 3507
And to maintain the state of the activity use onSavedInstanceState() and onRestoreInstanceSate() methods.
Upvotes: 0
Reputation: 71
I have the same problem!! Go to Settings/Development/ and uncheck 'Destroy Activities'
Upvotes: 7
Reputation: 2004
This is from the Android Activity documentation (Link here):
Perhaps points 3 and 4 are relevant to you.
Upvotes: 1