Question

In: Computer Science

Develop an Android app to document the lifecycle of an “activity” For each callback events (onCreate(),...

Develop an Android app to document the lifecycle of an “activity”

For each callback events (onCreate(), onStart()... ), record what callback event was triggered in the log(use Log.d); the message that is written to log should be defined in “strings.xml” and getResources.getString() should be used to retrieve the message.

Implement onSaveInstanceState and onRestoreInstanceState – to track the number of times onSaveInstanceState is being called, in onRestoreInstanceState, print the value to the log file.

Tag for “Log” statement should also be defined in strings.xml and set using getResources.getString()

To retrieve value of a string token, use getResources.getString() ; syntax is:
String greeting = getResources().getString(R.string.hello);
R is automatically generated and string is a static class, “hello” is the name of the string in “strings.xml”

Define a variable (global)– in onSaveInstanceState, increment it by 1 and save it to the bundle; in onRestoreInstanceState, set variable to the value stored in the bundle and print it to the log file.

Perform different actions like rotating the screen orientation, back button, home button, restore this lab app – analyze how your actions impact the activity lifecycle.

Solutions

Expert Solution

//activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

//Strings.xml

<resources>
    <string name="app_name">LogEvents</string>
    <string name="onCreate">onCreate() is called. </string>
    <string name="onStart">onStrat() is called.</string>
    <string name="OnPause">onPause() is called.</string>
    <string name="OnRestart">onRestart() is called</string>
    <string name="OnStop">onStop() is called.</string>
    <string name="onResume">onResume() is called.</string>
    <string name="onDestroy">onDestroy() is called.</string>
    <string name="onRestart">onRestart() is called.</string>
    <string name="Tag">LOG</string>
</resources>

//=====================================

//MainActivity.java

package com.example.logevents;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends AppCompatActivity {

    /**
     * calls when activity is first created
     * @param savedInstanceState
     */
    int counter=0;
    String logTag = " ";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        logTag = getResources().getString(R.string.Tag);
        Log.d(logTag, getString(R.string.onCreate));
    }

    /**
     * When activity is visible to user
     */
    @Override
    protected void onStart() {
        super.onStart();
        Log.d(logTag, getResources().getString(R.string.onStart));
    }
    //User interact with activity
    @Override
    protected void onResume() {
        super.onResume();
        Log.d(logTag, getResources().getString(R.string.onResume));
    }
    /**
     * When activity is closed
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(logTag, getResources().getString(R.string.onDestroy));
    }

    /**
     * Restart after stopping
     */
    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d(logTag, getResources().getString(R.string.onRestart));
    }

    /**
     * when activity is paused
     */
    @Override
    protected void onPause() {
        super.onPause();
        Log.d(logTag, getResources().getString(R.string.OnPause));
    }

    /**
     * Activity is not visible
     */
    @Override
    protected void onStop() {
        super.onStop();
        Log.d(logTag, getResources().getString(R.string.OnStop));
    }
    /**
     * Implement onSaveInstanceState and
     * onRestoreInstanceState – to track
     * the number of times onSaveInstanceState
     * is being called, in onRestoreInstanceState,
     * print the value to the log file.
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        counter++;
        outState.putString("counter", String.valueOf(counter));
    }

    @Override
    protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        if(savedInstanceState!=null)
        Log.d(logTag,savedInstanceState.getString("counter"));
    }
}

//==========================================

//Android menifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.logevents">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

//output

//If you need any help regarding this solution ............... please leave a comment ........... thanks


Related Solutions

Android Programming Create an app called GuessWho. The point of the app will to have the...
Android Programming Create an app called GuessWho. The point of the app will to have the user play a guessing game based on a displayed image. The user will be greeted with a screen that has an image of a person, four buttons and a next button. The four buttons should each have a different name on it. One of the names will be the actual name of the person in the image. Your guess who quiz should consist of...
Using Android Studio, create a one java android app that has 4 buttons:- Change Color Button...
Using Android Studio, create a one java android app that has 4 buttons:- Change Color Button - When the Change Color button is clicked, it changes the current activity background to a randomly selected color Speak Button - When the Speak button is clicked, it opens a new activity named SpeakActivity. On the SpeakActivity there are three controls: EditText, Button (Speak) and Button (Back). The Speak button uses the Text to Speech service to say the text entered in EditText....
Hi! I need it in android studio and in java Design a game app “BouncingBall ”...
Hi! I need it in android studio and in java Design a game app “BouncingBall ” in which the user’s goal is to prevent a bouncing ball from falling off the bottom of the screen. When the user presses the start button, a ball bounces off the top, left and right sides (the “walls”) of the screen. A horizontal bar on the bottom of the screen serves as a paddle to prevent the ball from hitting the bottom of the...
Hi! I need it in android studio and in java Design a game app “BouncingBall ”...
Hi! I need it in android studio and in java Design a game app “BouncingBall ” in which the user’s goal is to prevent a bouncing ball from falling off the bottom of the screen. When the user presses the start button, a ball bounces off the top, left and right sides (the “walls”) of the screen. A horizontal bar on the bottom of the screen serves as a paddle to prevent the ball from hitting the bottom of the...
android studio Begin a new app and create a Java class for products with data: productName,...
android studio Begin a new app and create a Java class for products with data: productName, productCode and price and use a file of objects to store product data and read back and display on screen. Do this by creating a simple form using EditTexts, buttons or ActionBar items and display output using an Alert.
Create a Scorekeeper app in android studio by using java language - Layouts | Widgets Create...
Create a Scorekeeper app in android studio by using java language - Layouts | Widgets Create the layout for your score keeping app. The app should have: Two team names (TextViews) Two scores (TextViews) Buttons to increase/ decrease the scores An amount to change the score by (RadioButtons) You must have at least two score options The scores can be changed by anything you want American football: 1, 2, 3, 6 Basketball: 1, 2, 3 Freestyle wrestling: 1, 2, 3,...
use android studio or MIT inventor Create a Presidents Quiz App for your project with the...
use android studio or MIT inventor Create a Presidents Quiz App for your project with the following requirements: You must have images Buttons Label Conditional Statement Homepage with instructions Sound (music, buzzer, etc.) - Ensure you are using the correct layout: Gravity(Android Studio), Horizontal or Vertical Arrangement (MIT). - 40POINTS Points System (indicating how many answers they got wrong/correct). 20POINT 1-2 questions per President (45+ questions)
For this IP, you will create a very simple drawing app using Android Studio. The purpose...
For this IP, you will create a very simple drawing app using Android Studio. The purpose of this assignment is to give you more building blocks to use when programming apps. For full credit for this assignment, you should complete the following: Create a menu and display menu items on the app bar Detect when the user touches the screen and moves a finger Be able to change the color and width of a line Be able to save an...
Linda sells an android app. Her firm’s production function is f (x1, x2) = x1 +...
Linda sells an android app. Her firm’s production function is f (x1, x2) = x1 + 2x2 where x1 is the amount of unskilled labor and x2 is the amount of skilled labor that she employs. Draw the isoquant at 20 units of output and another at 40 units of output. Does this production function exhibit increasing, decreasing, or constant return to scale? If Linda faces factor prices (w1, w2) = (1, 1), what is the cheapest way to produce...
I am doing a 4 function calculator app in java on android studio which calculates math...
I am doing a 4 function calculator app in java on android studio which calculates math expressions (add, subtract, multiply, and divide). I ONLY NEED THE CODE THE EQUAL BUTTON. When the user clicks the equal button, it should take the entire sequence of numbers and operators on the screen, such as 1+2*5 , and save them into a String [I am pretty sure this can be done using the toString() call]. Then, the String should be split using StringTokenizer,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT