Hey there, Here is an example of Activity life cycle, This will illustrate more about how onCreate(), onStart(), onResume, onPause(), onStop() call in activity Life Cycle, I have Created two activity and given toast in each method, when you navigate the application Toast will pop on the screen as per Activity Life cycle.
<!-- ================First layout (main.xml)=====================-->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/but"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
<!-- ===================End of main.xml====================== -->
/*=========ActivityLifeCycleActivity (Corrosponding code to main.xml) ========*/
package com.hellboys.act;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class ActivityLifeCycleActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Toast.makeText(getBaseContext(), "1 onCreate()",Toast.LENGTH_LONG).show();
Button b1;
b1 = (Button) findViewById(R.id.but);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(ActivityLifeCycleActivity.this,abhi.class);
startActivity(intent);
}
});
}
public void onStart(){
super.onStart();
Toast.makeText(getBaseContext(), "1 onStart()",Toast.LENGTH_LONG).show();
}
public void onResume(){
super.onResume();
Toast.makeText(getBaseContext(), "1 onResume()",Toast.LENGTH_LONG).show();
}
public void onPause(){
super.onPause();
Toast.makeText(getBaseContext(), "1 onPause()",Toast.LENGTH_LONG).show();
}
public void onStop(){
super.onStop();
Toast.makeText(getBaseContext(), "1 onStop()",Toast.LENGTH_LONG).show();
}
}
/*=============End of ActivityLifeCycleActivity=================*/
<!-- ================Second layout (abhi.xml)==================-->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<AnalogClock
android:id="@+id/analogClock1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<!-- =====================End of abhi.xml======================= -->
/*=========Second Activity(Java code corresponding to abhi.xml) ========= */
package com.hellboys.act;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
public class abhi extends Activity{
@Override
public void onCreate(Bundle savedInstance){
super.onCreate(savedInstance);
setContentView(R.layout.abhi);
Toast.makeText(getBaseContext(), "2 onCreate()", Toast.LENGTH_LONG).show();
}
public void onStart(){
super.onStart();
Toast.makeText(getBaseContext(), "2 start()", Toast.LENGTH_LONG).show();
}
public void onResume(){
super.onResume();
Toast.makeText(getBaseContext(), "2 onResume()", Toast.LENGTH_LONG).show();
}
public void onPause(){
super.onPause();
Toast.makeText(getBaseContext(), "2 onPause()", Toast.LENGTH_LONG).show();
}
public void onStop(){
super.onStop();
Toast.makeText(getBaseContext(), "2 onstop()", Toast.LENGTH_LONG).show();
}
}
/*=================End of abhi============================*/
<!-- ================First layout (main.xml)=====================-->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/but"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
<!-- ===================End of main.xml====================== -->
/*=========ActivityLifeCycleActivity (Corrosponding code to main.xml) ========*/
package com.hellboys.act;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class ActivityLifeCycleActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Toast.makeText(getBaseContext(), "1 onCreate()",Toast.LENGTH_LONG).show();
Button b1;
b1 = (Button) findViewById(R.id.but);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(ActivityLifeCycleActivity.this,abhi.class);
startActivity(intent);
}
});
}
public void onStart(){
super.onStart();
Toast.makeText(getBaseContext(), "1 onStart()",Toast.LENGTH_LONG).show();
}
public void onResume(){
super.onResume();
Toast.makeText(getBaseContext(), "1 onResume()",Toast.LENGTH_LONG).show();
}
public void onPause(){
super.onPause();
Toast.makeText(getBaseContext(), "1 onPause()",Toast.LENGTH_LONG).show();
}
public void onStop(){
super.onStop();
Toast.makeText(getBaseContext(), "1 onStop()",Toast.LENGTH_LONG).show();
}
}
/*=============End of ActivityLifeCycleActivity=================*/
<!-- ================Second layout (abhi.xml)==================-->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<AnalogClock
android:id="@+id/analogClock1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<!-- =====================End of abhi.xml======================= -->
/*=========Second Activity(Java code corresponding to abhi.xml) ========= */
package com.hellboys.act;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
public class abhi extends Activity{
@Override
public void onCreate(Bundle savedInstance){
super.onCreate(savedInstance);
setContentView(R.layout.abhi);
Toast.makeText(getBaseContext(), "2 onCreate()", Toast.LENGTH_LONG).show();
}
public void onStart(){
super.onStart();
Toast.makeText(getBaseContext(), "2 start()", Toast.LENGTH_LONG).show();
}
public void onResume(){
super.onResume();
Toast.makeText(getBaseContext(), "2 onResume()", Toast.LENGTH_LONG).show();
}
public void onPause(){
super.onPause();
Toast.makeText(getBaseContext(), "2 onPause()", Toast.LENGTH_LONG).show();
}
public void onStop(){
Toast.makeText(getBaseContext(), "2 onstop()", Toast.LENGTH_LONG).show();
}
}
/*=================End of abhi============================*/
/*==========================Do +1 if like ....... ========================*/
No comments:
Post a Comment