0% found this document useful (0 votes)
22 views5 pages

Activitymainxml

The document contains code for two Android activities and their layout files. It also includes code demonstrating the lifecycle of activities, with logging of lifecycle method calls in two simple activities.

Uploaded by

Sanika Shinde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views5 pages

Activitymainxml

The document contains code for two Android activities and their layout files. It also includes code demonstrating the lifecycle of activities, with logging of lifecycle method calls in two simple activities.

Uploaded by

Sanika Shinde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Activitymainxml

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Activity 1"
/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:id="@+id/btn"
/>

</LinearLayout>

Activity2.java

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Activity 2"
/>

</LinearLayout>

Mainactivity.java
package com.example.implicit_activity;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {

Button b;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

b=(Button) findViewById(R.id.btn);

b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i=new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("https://www.google.com"));
startActivity(i);
}
});
}
}

IMPLICIT

package com.example.explicit_activity;

import androidx.appcompat.app.AppCompatActivity;

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

import com.example.explicit_activity.R;

public class MainActivity extends AppCompatActivity {

Button b;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

b=(Button) findViewById(R.id.btn);

b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i=new Intent(MainActivity.this,Activity2.class);
startActivity(i);
}
});
}
}

life cycle

mainactivity.java

package com.example.life_cycle;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

Button b;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

b=(Button) findViewById(R.id.btn);

Log.d("Activity2","is onCreate()");

b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i=new Intent(MainActivity.this,Activity2.class);
startActivity(i);
}
});
}

@Override
protected void onStart() {
super.onStart();
Log.d("Activity1","In onStart()");
}

@Override
protected void onResume() {
super.onResume();
Log.d("Activity1", "In onResume()");
}

@Override
protected void onPause() {
super.onPause();
Log.d("Activity1","In onPause()");
}

@Override
protected void onStop() {
super.onStop();
Log.d("Activity1","In onStop()");
}

@Override
protected void onRestart() {
super.onRestart();
Log.d("Activity1","In onRestart()");
}

@Override
protected void onDestroy() {
super.onDestroy();
Log.d("Activity1","In onDestroy()");
}

activity2.java

package com.example.life_cycle;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

public class Activity2 extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);

Log.d("Activity2","In onCreate()");
}
@Override
protected void onStart() {
super.onStart();
Log.d("Activity2","In onStart()");
}

@Override
protected void onResume() {
super.onResume();
Log.d("Activity2", "In onResume()");
}

@Override
protected void onPause() {
super.onPause();
Log.d("Activity2","In onPause()");
}

@Override
protected void onStop() {
super.onStop();
Log.d("Activity2","In onStop()");
}

@Override
protected void onRestart() {
super.onRestart();
Log.d("Activity2","In onRestart()");
}

@Override
protected void onDestroy() {
super.onDestroy();
Log.d("Activity2","In onDestroy()");
}

You might also like