Hello friends,
Welcome you all
in the “LIFECYCLE of an Activity” part 3.
In the part2 we have learnt, how to use onCreate(), onStart() and
onResume() system call. We have also seen how the Activity comes into RESUME
(visible) state.
Click the following link to refer:-
In this part3, we will learn, how to use onPause(), onStop() and
onDestroy() system call. We will also learn how state change from RESUME to
PAUSED, PAUSED to STOP and STOP to DESTROY.
Step1: User Interface
of MainActivity
Add
one more Button into the MainActivity name as “Start Sub Activity”.
Please
add the following code into the activity_main.xml file.
Step2: Create a
New Activity
File à New à Other à Android à
AndroidActivity
Select BlankActivity, click Next.
Project:
LifeCycleDemo
Activity name: SubActivity
Click Finish.
Step3: Implement onPause(), onStop(),
onDestroy() system calls
Define
the onPause(), onStop() and onDestroy() system call into the MainActivity.
Create
a function that handles the “Start Sub Acivity” Button click event.
Please
add the following code into the MainActivity.java file:-
@Override protected void onPause() { super.onPause(); mStatusView.append("MainActivity: onPause()\n"); mStatusView.append("MainActivity: In PAUSED state ---\n"); } @Override protected void onStop() { super.onStop(); mStatusView.append("MainActivity: onStop()\n"); mStatusView.append("MainActivity: In STOP state ---\n"); } @Override protected void onDestroy() { super.onDestroy(); mStatusView.append("MainActivity: onDestroy()\n"); } public void startSubActivity(View v) { Intent intent = new Intent(MainActivity.this, SubActivity.class); startActivity(intent); }
Step4: Implement all the lifecycle system calls
for SubActivity
Please modify the SubActivity.java file as
follows:-
package com.example.lifecycledemo; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.TextView; public class SubActivity extends Activity { private TextView mStatusView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sub); mStatusView = (TextView) findViewById(R.id.status_current); mStatusView.setText("SubActivity: onCreate()\n"); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_sub, menu); return true; } protected void onStart() { super.onStart(); mStatusView.append("SubActivity: onStart()\n"); } protected void onResume() { super.onResume(); mStatusView.append("SubActivity: onResume()\n"); mStatusView.append("SubActivity: In RESUME state ---\n"); } @Override protected void onPause() { super.onPause(); mStatusView.append("SubActivity: onPause()\n"); mStatusView.append("SubActivity: In PAUSED state ---\n"); } @Override protected void onStop() { super.onStop(); mStatusView.append("SubActivity: onStop()\n"); mStatusView.append("SubActivity: In STOP state ---\n"); } @Override protected void onDestroy() { super.onDestroy(); mStatusView.append("SubActivity: onDestroy()\n"); } public void destroyActivity(View v) { SubActivity.this.finish(); } }
Step 5: Design User Interface for SubActivity
Please
modify the activity_sub.xml file as follows:-
Step 6: Compile and RUN the application.
Click the Start Sub_Activity Button, Now the State of MainActivity and State of SubActivity are as follows:-
Destroy the SubActivity:-
Please give your valuable
comments.
Do not forgot to share !!!
No comments:
Post a Comment