Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.57 KB | None | 0 0
  1. package com.example.myapplication;
  2.  
  3. import androidx.appcompat.app.AppCompatActivity;
  4.  
  5. import android.app.Activity;
  6. import android.content.Context;
  7. import android.content.SharedPreferences;
  8. import android.nfc.Tag;
  9. import android.os.Bundle;
  10. import android.util.Log;
  11. import android.view.View;
  12. import android.widget.TextView;
  13.  
  14. import org.w3c.dom.Text;
  15.  
  16. import java.util.zip.CheckedOutputStream;
  17.  
  18. public class MainActivity extends AppCompatActivity {
  19. private String lastTextSaved;
  20.  
  21. private final String TAG = "Logging Example";
  22. private final String creationsTAG = "onCreate()";
  23. private final String visiblesTAG = "onStart()";
  24. private final String foregroundsTAG = "onResume()";
  25. private final String ACTIVITY_TAG = "Is activated";
  26.  
  27. private int onCreateCount;
  28. private int onStartCount;
  29. private int onResumeCount;
  30. TextView view, view2, view3;
  31.  
  32. private static final String sharedPref = "com.example.android.MyApplication3";
  33.  
  34. Counter creationsCounter = new Counter();
  35. Counter visiblesCounter = new Counter();
  36. Counter foregroundsCounter = new Counter();
  37.  
  38. private SharedPreferences prefGet;
  39.  
  40. @Override
  41. public void onCreate(Bundle savedInstanceState) {
  42. super.onCreate(savedInstanceState);
  43. setContentView(R.layout.activity_main);
  44. view = (TextView) findViewById(R.id.numberCreations);
  45. view2 = (TextView) findViewById(R.id.numberVisibles);
  46. view3 = (TextView) findViewById(R.id.numberForegrounds);
  47.  
  48. Log.d(TAG, Integer.toString(onCreateCount));
  49. Log.d(TAG, "onCreate() called");
  50.  
  51. prefGet = getSharedPreferences("sharedPref" ,Activity.MODE_PRIVATE);
  52. SharedPreferences.Editor prefEditor = prefGet.edit();
  53.  
  54. if(prefGet.contains(ACTIVITY_TAG)) {
  55. creationsCounter.setCounter(prefGet.getInt(creationsTAG, onCreateCount));
  56. visiblesCounter.setCounter(prefGet.getInt(visiblesTAG, onStartCount));
  57. foregroundsCounter.setCounter(prefGet.getInt(foregroundsTAG, onResumeCount));
  58. } else {
  59. prefGet.getInt(creationsTAG, 0);
  60. prefGet.getInt(visiblesTAG, 0);
  61. prefGet.getInt(foregroundsTAG, 0);
  62. prefEditor.putString(ACTIVITY_TAG, "Activated");
  63. prefEditor.apply();
  64. }
  65. creationsCounter.plusCount();
  66. updateUI();
  67. Log.d(TAG, Integer.toString(onCreateCount));
  68. }
  69.  
  70. public void resetPressed(View view){
  71. SharedPreferences.Editor prefEditor = prefGet.edit();
  72. prefEditor.clear();
  73. creationsCounter.resetCount();
  74. visiblesCounter.resetCount();
  75. foregroundsCounter.resetCount();
  76. prefEditor.apply();
  77. updateUI();
  78. }
  79.  
  80. public void onStart() {
  81. super.onStart();
  82. visiblesCounter.plusCount();
  83. Log.d(TAG, "onStart() called");
  84. TextView view2 = findViewById(R.id.numberVisibles);
  85. updateUI();
  86. }
  87.  
  88. public void onResume() {
  89. super.onResume();
  90. foregroundsCounter.plusCount();
  91. TextView view3 = findViewById(R.id.numberForegrounds);
  92. Log.d(TAG, "onResume() called");
  93. updateUI();
  94. }
  95.  
  96. public void onPause() {
  97. super.onPause();
  98.  
  99. prefGet = getSharedPreferences("sharedPref", Activity.MODE_PRIVATE);
  100. SharedPreferences.Editor prefEditor = prefGet.edit();
  101.  
  102. onCreateCount = creationsCounter.getCounter();
  103. onStartCount = visiblesCounter.getCounter();
  104. onResumeCount = foregroundsCounter.getCounter();
  105.  
  106. prefEditor.putInt(creationsTAG, onCreateCount);
  107. prefEditor.putInt(visiblesTAG, onStartCount);
  108. prefEditor.putInt(foregroundsTAG, onResumeCount);
  109. prefEditor.apply();
  110.  
  111. Log.d(TAG, "onPause() called");
  112. }
  113.  
  114. public void onStop() {
  115. Log.d(TAG, "onStop() called");
  116. super.onStop();
  117. updateUI();
  118. }
  119.  
  120. public void onDestroy() {
  121. Log.d(TAG, "onDestroy() called");
  122. super.onDestroy();
  123. updateUI();
  124. }
  125.  
  126. public void updateUI(){
  127. view.setText(Integer.toString(creationsCounter.getCounter()));
  128. view2.setText(Integer.toString(visiblesCounter.getCounter()));
  129. view3.setText(Integer.toString(foregroundsCounter.getCounter()));
  130. }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement