Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.myapplication;
- import androidx.appcompat.app.AppCompatActivity;
- import android.app.Activity;
- import android.content.Context;
- import android.content.SharedPreferences;
- import android.nfc.Tag;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.TextView;
- import org.w3c.dom.Text;
- import java.util.zip.CheckedOutputStream;
- public class MainActivity extends AppCompatActivity {
- private String lastTextSaved;
- private final String TAG = "Logging Example";
- private final String creationsTAG = "onCreate()";
- private final String visiblesTAG = "onStart()";
- private final String foregroundsTAG = "onResume()";
- private final String ACTIVITY_TAG = "Is activated";
- private int onCreateCount;
- private int onStartCount;
- private int onResumeCount;
- TextView view, view2, view3;
- private static final String sharedPref = "com.example.android.MyApplication3";
- Counter creationsCounter = new Counter();
- Counter visiblesCounter = new Counter();
- Counter foregroundsCounter = new Counter();
- private SharedPreferences prefGet;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- view = (TextView) findViewById(R.id.numberCreations);
- view2 = (TextView) findViewById(R.id.numberVisibles);
- view3 = (TextView) findViewById(R.id.numberForegrounds);
- Log.d(TAG, Integer.toString(onCreateCount));
- Log.d(TAG, "onCreate() called");
- prefGet = getSharedPreferences("sharedPref" ,Activity.MODE_PRIVATE);
- SharedPreferences.Editor prefEditor = prefGet.edit();
- if(prefGet.contains(ACTIVITY_TAG)) {
- creationsCounter.setCounter(prefGet.getInt(creationsTAG, onCreateCount));
- visiblesCounter.setCounter(prefGet.getInt(visiblesTAG, onStartCount));
- foregroundsCounter.setCounter(prefGet.getInt(foregroundsTAG, onResumeCount));
- } else {
- prefGet.getInt(creationsTAG, 0);
- prefGet.getInt(visiblesTAG, 0);
- prefGet.getInt(foregroundsTAG, 0);
- prefEditor.putString(ACTIVITY_TAG, "Activated");
- prefEditor.apply();
- }
- creationsCounter.plusCount();
- updateUI();
- Log.d(TAG, Integer.toString(onCreateCount));
- }
- public void resetPressed(View view){
- SharedPreferences.Editor prefEditor = prefGet.edit();
- prefEditor.clear();
- creationsCounter.resetCount();
- visiblesCounter.resetCount();
- foregroundsCounter.resetCount();
- prefEditor.apply();
- updateUI();
- }
- public void onStart() {
- super.onStart();
- visiblesCounter.plusCount();
- Log.d(TAG, "onStart() called");
- TextView view2 = findViewById(R.id.numberVisibles);
- updateUI();
- }
- public void onResume() {
- super.onResume();
- foregroundsCounter.plusCount();
- TextView view3 = findViewById(R.id.numberForegrounds);
- Log.d(TAG, "onResume() called");
- updateUI();
- }
- public void onPause() {
- super.onPause();
- prefGet = getSharedPreferences("sharedPref", Activity.MODE_PRIVATE);
- SharedPreferences.Editor prefEditor = prefGet.edit();
- onCreateCount = creationsCounter.getCounter();
- onStartCount = visiblesCounter.getCounter();
- onResumeCount = foregroundsCounter.getCounter();
- prefEditor.putInt(creationsTAG, onCreateCount);
- prefEditor.putInt(visiblesTAG, onStartCount);
- prefEditor.putInt(foregroundsTAG, onResumeCount);
- prefEditor.apply();
- Log.d(TAG, "onPause() called");
- }
- public void onStop() {
- Log.d(TAG, "onStop() called");
- super.onStop();
- updateUI();
- }
- public void onDestroy() {
- Log.d(TAG, "onDestroy() called");
- super.onDestroy();
- updateUI();
- }
- public void updateUI(){
- view.setText(Integer.toString(creationsCounter.getCounter()));
- view2.setText(Integer.toString(visiblesCounter.getCounter()));
- view3.setText(Integer.toString(foregroundsCounter.getCounter()));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement