3

I have some Activities, let's say Activity A, B and C. In the Activity A I call the B through a Menu with an onOptionsItemSelected:

Intent main = new Intent (this, MainActivity.class);
this.startActivity(main);

Now, when I'm in the B activity, I can call the A one back in the same way (with Intent and startactivity): how can i handle it to call the OnResume or the OnRestart method of A instead of the OnCreate one?

I'm logging it and anytime I move from an activity to another one, it always call the OnCreate method: what can I do?

StarsSky
  • 6,721
  • 6
  • 38
  • 63
peppeuz
  • 231
  • 5
  • 13
  • Are you done with Activity B and want to go back to A? Or do you want to keep B around for some reason? If its the first, just call finish() and end B. – Gabe Sechan Feb 03 '13 at 22:03
  • 1
    I suggest you to study this tutorial and code example http://developer.android.com/training/basics/activity-lifecycle/index.html – StarsSky Feb 03 '13 at 22:04

3 Answers3

2

Configure your Activity A as "singleTask" or "singleInstance" in the manifest.xml. Then Acitivity A's onResume() will be fire instead of onCreate() when you call Activity A from Activity B (assuming Activity A was already instantiated like you describe). There are drawbacks to this kind of configuration so read this.

example manifest:

<activity android:name=".YourActivityA"     
        android:configChanges="keyboardHidden|orientation"      
        android:launchMode="singleTask">
0

You cannot control whether the OnCreate/OnResume/OnRestart methods are called directly. It is based on whether the previous activity is still in memory or not.

http://developer.android.com/guide/components/activities.html http://developer.android.com/images/activity_lifecycle.png

Aelexe
  • 1,216
  • 3
  • 18
  • 27
  • 1
    [You can.](http://stackoverflow.com/questions/13964943/why-do-oncreate-should-be-called-only-once-on-the-start-of-activity/13964977#13964977) I've tested it. – wtsang02 Feb 03 '13 at 22:08
  • 3
    Just because you can does not mean that you should. Calling onCreate() manually is a massive code smell to me and indicates bad design and misuse of the platform. – Simon Feb 03 '13 at 22:12
0

I'm logging it and anytime I move from an activity to another one, it always call the OnCreate method: what can I do?

To return to Activity A from Activity B, if you use:

  • startActivity(), then A's onCreate() will always be called.
  • finish(), then A's onCreate() may or may not be called. (It depends on whether A was destroyed by you or the OS.)
Sam
  • 86,580
  • 20
  • 181
  • 179