0

which method is called in current activity when moving to another activity? Let's take i am in Activity A, from A i move to Activity B.

I want to execute a particular piece of code while moving to Activity B

theJava
  • 14,620
  • 45
  • 131
  • 172
  • 7
    you should atleast read activity life cylcle , just google it and you will get your answer. – Prateek May 28 '13 at 09:36
  • When you navigate from activity A to B. Activity A is paused. So onPause() is called – Raghunandan May 28 '13 at 09:37
  • You need to learn Activity Life cycle ,check this [link](http://stackoverflow.com/questions/8515936/android-activity-life-cycle-what-are-all-these-methods-for) – MohsinSyd May 28 '13 at 09:39

5 Answers5

2

You can write your code inside Activity B's onCreate().

Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
2

Nothing is called on your current activity specifically for "moving to another activity".

Your current activity will be called with onPause() when it leaves the foreground from an input standpoint, and it will be called with onStop() when it is no longer visible. However, those will be called for any trigger for those events, including the user pressing HOME or BACK.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
1

onPause and probably onStop (if the new activity takes up the entire screen) will be called on A. onDestroy 'can' be called at this point, but the system will keep it alive if it can.

onCreate, onStart, onResume will be called on B, unless B is already in the stack and you either use some flags in the intent, or B has a special launchMode, in which case, rather than onCreate, onNewIntent will be called.

FunkTheMonk
  • 10,908
  • 1
  • 31
  • 37
1

onPause() method is calledwhen it leaves the foreground and onStop() method is called when the activity is no longer visible.. Also refer to the complete activity life cycle in the below mentioned link

Refer to table 1 of the activity life cycle in this link Activity Life cycle

Srikanth Pai
  • 926
  • 3
  • 17
  • 30
1

Activity life cycle

onPause() and then onStop() methods will call when you change the activity ,,if you finish the current activity while moving to the second activity the onDestory() also called..

Here is an intrusting issue that if you start an your new activity in onStop method the onPause and onStop method wont call again :)

Sandeep P
  • 4,291
  • 2
  • 26
  • 45