125 lines
3.7 KiB
Java
125 lines
3.7 KiB
Java
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();
|
|
}
|
|
}
|