Загрузить файлы в «/»
This commit is contained in:
commit
9ab7e25747
124
MainActivity.java
Normal file
124
MainActivity.java
Normal file
@ -0,0 +1,124 @@
|
||||
package com.example.lifecyclelogger;
|
||||
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
|
||||
private TextView lifecycleTextView;
|
||||
private List<String> lifecycleEvents;
|
||||
private SimpleDateFormat sdf;
|
||||
private static final String PREF_NAME = "LifecycleLoggerPrefs";
|
||||
private static final String KEY_LIFECYCLE_EVENTS = "lifecycle_events";
|
||||
|
||||
@Override
|
||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
|
||||
lifecycleTextView = findViewById(R.id.lifecycleTextView);
|
||||
sdf = new SimpleDateFormat("HH:mm:ss.SSS", Locale.getDefault());
|
||||
|
||||
// Initialize lifecycle events list
|
||||
lifecycleEvents = new ArrayList<>();
|
||||
|
||||
// Load saved lifecycle events
|
||||
loadLifecycleEvents();
|
||||
updateLifecycleTextView();
|
||||
|
||||
logAndDisplayLifecycleEvent("onCreate");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStart() {
|
||||
super.onStart();
|
||||
logAndDisplayLifecycleEvent("onStart");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
logAndDisplayLifecycleEvent("onResume");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
super.onPause();
|
||||
logAndDisplayLifecycleEvent("onPause");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStop() {
|
||||
super.onStop();
|
||||
logAndDisplayLifecycleEvent("onStop");
|
||||
|
||||
// Save lifecycle events when the activity is stopped
|
||||
saveLifecycleEvents();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
logAndDisplayLifecycleEvent("onDestroy");
|
||||
}
|
||||
|
||||
private void logAndDisplayLifecycleEvent(String event) {
|
||||
String timestamp = sdf.format(new Date());
|
||||
String logEntry = timestamp + " - " + event;
|
||||
lifecycleEvents.add(logEntry);
|
||||
updateLifecycleTextView();
|
||||
}
|
||||
|
||||
private void updateLifecycleTextView() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (String e : lifecycleEvents) {
|
||||
sb.append(e).append("\n");
|
||||
}
|
||||
lifecycleTextView.setText(sb.toString());
|
||||
}
|
||||
|
||||
private void saveLifecycleEvents() {
|
||||
SharedPreferences prefs = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
|
||||
SharedPreferences.Editor editor = prefs.edit();
|
||||
Set<String> eventSet = new HashSet<>(lifecycleEvents); // Convert list to set to avoid duplicates
|
||||
editor.putStringSet(KEY_LIFECYCLE_EVENTS, eventSet);
|
||||
editor.apply();
|
||||
}
|
||||
|
||||
private void loadLifecycleEvents() {
|
||||
SharedPreferences prefs = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
|
||||
Set<String> eventSet = prefs.getStringSet(KEY_LIFECYCLE_EVENTS, new HashSet<>());
|
||||
lifecycleEvents = new ArrayList<>(eventSet); // Convert set back to list
|
||||
}
|
||||
|
||||
public void onClearHistoryClicked(View view) {
|
||||
// Clear saved events in SharedPreferences
|
||||
SharedPreferences prefs = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
|
||||
SharedPreferences.Editor editor = prefs.edit();
|
||||
editor.remove(KEY_LIFECYCLE_EVENTS);
|
||||
editor.apply();
|
||||
|
||||
// Clear lifecycle events list
|
||||
lifecycleEvents.clear();
|
||||
updateLifecycleTextView();
|
||||
|
||||
// Add button click event to lifecycle events
|
||||
logAndDisplayLifecycleEvent("Clear History button clicked");
|
||||
|
||||
// Save the new log entry
|
||||
saveLifecycleEvents();
|
||||
}
|
||||
}
|
26
activity_main.xml
Normal file
26
activity_main.xml
Normal file
@ -0,0 +1,26 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:context=".MainActivity">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/lifecycleTextView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:textSize="18sp"
|
||||
android:padding="16dp"
|
||||
android:text="Lifecycle events will be displayed here" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/clearHistoryButton"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Clear History"
|
||||
android:layout_alignParentBottom="true"
|
||||
android:layout_centerHorizontal="true"
|
||||
android:layout_marginBottom="16dp"
|
||||
android:onClick="onClearHistoryClicked" />
|
||||
|
||||
</RelativeLayout>
|
Loading…
Reference in New Issue
Block a user