Activity Lifecycle

By ukmodak | March 31st 2024 10:30:09 AM | viewed 999 times

create a main.xml layout and Add a button to show life cycle activity in the location: layout(android studio)

<?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" >


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >


        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="426dp" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:text="Welcome to main activity "
                android:textColor="#ffffff"
                android:textSize="15sp" />

            <Button
                android:id="@+id/btnLifeCycle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_below="@+id/textView1"
                android:layout_marginLeft="18dp"
                android:layout_marginTop="14dp"
                android:text="Activity life cycle"
                android:textSize="8sp" />
          

        </RelativeLayout>

    </LinearLayout>

</LinearLayout>

create a ProjectonOverallClassMainActivity activity to send request to lifyCycle activity

package com.uk.modak;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class ProjectonOverallClassMainActivity extends Activity {
   
	
	private Button btnLifeCycle;
		
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
               
        btnLifeCycle = (Button) findViewById(R.id.btnLifeCycle);             
        btnLifeCycle.setOnClickListener(new View.OnClickListener() {			
			public void onClick(View v) {				
				Intent x = new Intent(ProjectonOverallClassMainActivity.this,LifyCycle.class);

				startActivity(x);
				
				
			}
		});                             
    }
    
    public void onStart(){
    	super.onStart();
    	 	
    }
      
    
}

create a showlifecycle.xml layout to view life cycle activity method sequence in the location: layout(android studio)

<?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" >


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:text="Welcome to lify cycle activity" />

    </RelativeLayout>

</LinearLayout>

create an activity in the location: src/main/java/com/uk/modak/LifeCycle (android studio)


package com.uk.modak;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;


public class LifyCycle extends Activity {
	
	public static final String EXTRA_GET_REQUEST_IS_TRUE ="";
	public static final String SHOW_RESULT_ON_MAIN = "";
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.showlifecycle);  

		
        Toast.makeText(LifyCycle.this, "run onCreate action", Toast.LENGTH_LONG).show();  
        
    }
	
	public void onStart(){
    	super.onStart();
    	Toast.makeText(LifyCycle.this, "run onStart action", Toast.LENGTH_LONG).show();
    	
    }
    
    public void onResume(){
    	super.onResume();
    	Toast.makeText(LifyCycle.this, "run onResume action", Toast.LENGTH_LONG).show();
    	
    }
    
    public void onPause(){
    	super.onPause();
    	Toast.makeText(LifyCycle.this, "run onPause action", Toast.LENGTH_LONG).show();
    }
    
    public void onStop(){
    	super.onStop();
    	Toast.makeText(LifyCycle.this, "run onStop action", Toast.LENGTH_LONG).show();
    }
    public void onDestroy(){
    	super.onDestroy();
    	Toast.makeText(LifyCycle.this, "run onDestroy action", Toast.LENGTH_LONG).show();
    	
    }

}

Go to menifeast and setup activity that we use

<application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".ProjectonOverallClassMainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
       
         <activity
            android:name=".LifyCycle"
            android:label="@string/app_name" >
        <span></activity>
        
        <receiver android:name=".ConnectionReceive">
            <intent-filter>
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            </intent-filter>
        </receiver>
        
    </application>
bONEandALL
Visitor

Total : 26654

Today :3

Today Visit Country :

  • France
  • United States