day 4: activity lifecycle

5
ACTIVITY Activity Life Cycle:

Upload: ahsanul-karim

Post on 03-Sep-2014

2.809 views

Category:

Documents


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Day 4: Activity lifecycle

ACTIVITY

Activity Life Cycle:

Page 2: Day 4: Activity lifecycle

onCreate

Called when your activity is first created. This is the place you normally create your views, open any persistent datafiles your activity needs to use, and in general initialize your activity. When calling onCreate, the Android framework is passed a Bundle object that contains any activity state saved from when the activity ran before.

@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);

Log.i(TAG,”On Create”);}

onResume

Called right after onStart if your activity is the foreground activity on the screen. At this point your activity is running and interacting with the user. You are receiving keyboard and touch inputs, and the screen is displaying your user interface. onResume is also called if your activity loses the foreground to another activity, and that activity eventually exits, popping your activity back to the foreground. This is where your activity would start (or resume) doing things that are needed to update the user interface (receiving location updates or running an animation, for example).

@Overrideprotected void onResume() {// TODO Auto-generated method stubsuper.onResume();Log.i(TAG,”On Resume”);}

onPause

Called when Android is just about to resume a different activity, giving that activity the foreground. At this point your activity will no longer have access to the screen, so you should stop doing things that consume battery and CPU cycles unnecessarily. If you are running an animation, no one is going to be able to see it, so you might as well suspend it until you get the screen back. Your activity needs to take advantage of this method to store any state that you will need in case your activity gains the foreground again—and it is not guaranteed that your activity will resume. If the mobile device you are running on runs out of memory, there is no virtual memory on disk to use for expansion, so your activity may have to make way for a system process that needs memory. Once you exit this method, Android may kill your activity at any time without returning control to you.

@Overrideprotected void onPause() {// TODO Auto-generated method stubsuper.onPause();

Page 3: Day 4: Activity lifecycle

Log.i(TAG,”On Pause”);}

onStop

Called when your activity is no longer visible, either because another activity has taken the foreground or because your activity is being destroyed.

@Overrideprotected void onStop() {// TODO Auto-generated method stubsuper.onStop();Log.i(TAG,”On Stop”);}

onDestroy

The last chance for your activity to do any processing before it is destroyed. Normally you'd get to this point because the activity is done and the framework called its finish method. But as mentioned earlier, the method might be called because Android has decided it needs the resources your activity is consuming.

@Overrideprotected void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy();

Log.i(TAG,”On Destroy”);}

From One Activity to another activity:

Suppose we want to go mainActivity to nextActivity

Add this activity in AndroidManifest.xml file<application android:icon="@drawable/icon" android:label="@string/app_name"><activity android:name="clubNewsActivity" ></activity></application>

So In mainActivity:

Page 4: Day 4: Activity lifecycle

Intent i = new Intent(mainActivity.this, nextActivity.class);

startActivity(i);

Value pass one to activity to another

If you want to pass some value from this activity to next activity.You have to do below:

In mainActivity:

Intent i = new Intent(mainActivity.this, nextActivity.class);

i.putExtra("id", "2");

i.putExtra("Name", "Karim");

startActivity(i);

In nextActivity:

@Override public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

String Id=getIntent().getStringExtra("id");

String Name=getIntent().getStringExtra("Name");

}