Guest User

Untitled

a guest
Dec 1st, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. private static final String TAG = "MainActivity";
  2.  
  3.  
  4. private MainPresenter mMainPresenter;
  5.  
  6.  
  7.  
  8. private TextView mTextView_ShowVal;
  9.  
  10. private Button mButton_IncreaseVal;
  11.  
  12. private EditText mEditText_SetNum;
  13.  
  14. private Button mButton_SetEditTextVal;
  15.  
  16.  
  17.  
  18.  
  19. private TextView mTextViewSomeText;
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26. @Override
  27. protected void onCreate(Bundle savedInstanceState)
  28. {
  29.  
  30. Log.d(TAG, "ON CREATE");
  31.  
  32. super.onCreate(savedInstanceState);
  33. setContentView(R.layout.activity_main);
  34.  
  35.  
  36. bindXML();
  37.  
  38.  
  39.  
  40.  
  41.  
  42. read();
  43.  
  44.  
  45. if (mMainPresenter == null)
  46. {
  47. mMainPresenter = new MainPresenterImpl(this);
  48. }
  49. else
  50. {
  51. mMainPresenter.setNewViewObj(this);
  52. }
  53.  
  54.  
  55. mMainPresenter.onCreate();
  56.  
  57.  
  58.  
  59.  
  60. setListeners();
  61.  
  62.  
  63.  
  64.  
  65.  
  66. }
  67.  
  68.  
  69. public void read(){
  70. Log.d(TAG, "read()");
  71. ObjectInputStream input;
  72. String filename = "testFilemost.srl";
  73.  
  74. try {
  75. input = new ObjectInputStream(new FileInputStream(new File(new File(getFilesDir(),"")+File.separator+filename)));
  76. mMainPresenter = (MainPresenter) input.readObject();
  77.  
  78. input.close();
  79. } catch (StreamCorruptedException e) {
  80. e.printStackTrace();
  81. } catch (FileNotFoundException e) {
  82. e.printStackTrace();
  83. } catch (IOException e) {
  84. e.printStackTrace();
  85. } catch (ClassNotFoundException e) {
  86. e.printStackTrace();
  87. }
  88.  
  89.  
  90.  
  91. }
  92.  
  93.  
  94.  
  95. public void write(){
  96. Log.d(TAG, "write()");
  97. String filename = "testFilemost.srl";
  98. ObjectOutput out = null;
  99.  
  100.  
  101.  
  102. try {
  103. out = new ObjectOutputStream(new FileOutputStream(new File(getFilesDir(),"")+File.separator+filename));
  104. out.writeObject(mMainPresenter);
  105. out.close();
  106. } catch (FileNotFoundException e) {
  107. e.printStackTrace();
  108. } catch (IOException e) {
  109. e.printStackTrace();
  110. }
  111. }
  112.  
  113.  
  114. @Override
  115. protected void onPause()
  116. {
  117. Log.d(TAG, "ON PAUSE");
  118.  
  119. write();
  120. mMainPresenter = null;
  121. read();
  122. super.onPause();
  123. }
  124.  
  125. @Override
  126. protected void onStart()
  127. {
  128. Log.d(TAG, "ON START");
  129.  
  130. read();
  131.  
  132. super.onStart();
  133. }
  134.  
  135. @Override
  136. protected void onStop()
  137. {
  138. Log.d(TAG, "ON STOP");
  139.  
  140.  
  141.  
  142.  
  143.  
  144. super.onStop();
  145. }
  146.  
  147. @Override
  148. protected void onDestroy()
  149. {
  150. Log.d(TAG, "ON DESTROY");
  151. super.onDestroy();
  152. }
  153.  
  154. @Override
  155. protected void onResume()
  156. {
  157. Log.d(TAG, "ON RESUME");
  158.  
  159. mMainPresenter.onResume();
  160.  
  161. super.onResume();
  162. }
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174. @Override
  175. public String getTextEditText()
  176. {
  177. return mEditText_SetNum.getText().toString();
  178. }
  179.  
  180. @Override
  181. public void setTextTextView(String text)
  182. {
  183. Log.d(TAG, "text = " + text);
  184. mTextView_ShowVal.setText(text);
  185. }
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198. public void setListeners()
  199. {
  200. mButton_IncreaseVal.setOnClickListener(new View.OnClickListener()
  201. {
  202. @Override
  203. public void onClick(View v)
  204. {
  205. mMainPresenter.executeClickIncreaseBtn();
  206. }
  207. });
  208.  
  209.  
  210.  
  211.  
  212. mButton_SetEditTextVal.setOnClickListener(new View.OnClickListener()
  213. {
  214. @Override
  215. public void onClick(View v)
  216. {
  217. mMainPresenter.executeClickSetValBtn();
  218. }
  219. });
  220. }
  221.  
  222.  
  223.  
  224.  
  225.  
  226. public void bindXML()
  227. {
  228. mTextView_ShowVal = (TextView) findViewById(R.id.textView_ShowNum);
  229. mButton_IncreaseVal = (Button) findViewById(R.id.button_IncreaseNum);
  230. mEditText_SetNum = (EditText) findViewById(R.id.editText_SetNum);
  231. mButton_SetEditTextVal = (Button) findViewById(R.id.button_SetEditTextVal);
  232.  
  233. mTextViewSomeText = (TextView) findViewById(R.id.someText);
  234. }
  235.  
  236.  
  237.  
  238. }
  239.  
  240. public class MainPresenterImpl implements MainPresenter, Serializable
  241. {
  242.  
  243.  
  244. private static final String TAG = "MainPresenterImpl";
  245.  
  246.  
  247.  
  248. private MainView mMainView;
  249. private ExperimentObject mValueObject = new ExperimentObject(0);
  250.  
  251.  
  252.  
  253. public MainPresenterImpl(MainActivity mainActivity)
  254. {
  255. mMainView = mainActivity;
  256. }
  257.  
  258.  
  259.  
  260.  
  261. @Override
  262. public void onCreate()
  263. {
  264.  
  265. }
  266.  
  267.  
  268. @Override
  269. public void onResume()
  270. {
  271.  
  272. }
  273.  
  274.  
  275. @Override
  276. public void setNewViewObj(MainView newViewObj)
  277. {
  278. mMainView = newViewObj;
  279. mMainView.setTextTextView(Integer.toString(mValueObject.getValue()));
  280. }
  281.  
  282. @Override
  283. public void executeClickIncreaseBtn()
  284. {
  285. mValueObject.setValue( mValueObject.getValue() + 1 );
  286. mMainView.setTextTextView(Integer.toString(mValueObject.getValue()));
  287. }
  288.  
  289. @Override
  290. public void executeClickSetValBtn()
  291. {
  292. String valueStr = mMainView.getTextEditText().toString();
  293. int value = Integer.parseInt(valueStr);
  294. mValueObject.setValue(value);
  295. mMainView.setTextTextView(valueStr);
  296. }
  297. }
Add Comment
Please, Sign In to add comment