I am trying to create a time tracking application that I can use at work. I pasted the code for my two classes below. My puzzle should not transmit or store data only, how to return to its original form if I opened a canvas object. Is it possible to use the intention to open activity, and not just create a "tv" object (in the openTEV method)? This will mean that when I press the back button in the lower right corner of my Galaxy phone, it will stop this activity and return to the first using the EditText objects into which I entered the lines. At the moment, pressing the back button terminates the entire application. Or I don’t understand how android works?
MainActivity.java -
public class MainActivity extends Activity {
private MyTouchEventView tv;
private EditText et1;
private EditText et2;
private EditText et3;
private EditText et4;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1 = (EditText) findViewById(R.id.editText1);
et2 = (EditText) findViewById(R.id.editText2);
et3 = (EditText) findViewById(R.id.editText3);
et4 = (EditText) findViewById(R.id.editText4);
}
public void processForm(View v) {
String txt = et1.getText().toString();
Toast.makeText(this.getApplicationContext(), txt, Toast.LENGTH_LONG).show();
}
@Override
protected void onPause() {
super.onPause();
Log.i("TAG", "Paused");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_save:
saveCanvasImage();
return true;
case R.id.action_sign:
openTEV();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void saveCanvasImage() {
tv.setDrawingCacheEnabled(true);
Bitmap bm = tv.getDrawingCache();
String fPath = Environment.getExternalStorageDirectory().toString();
fPath = fPath + "/Pictures";
File f = null;
String s = getCurrDate();
s = s + ".png";
f = new File(fPath, s);
try {
FileOutputStream strm = new FileOutputStream(f);
bm.compress(CompressFormat.PNG, 80, strm);
strm.flush();
strm.close();
}
catch (IOException e){
e.printStackTrace();
}
}
public void openTEV() {
tv = new MyTouchEventView(this);
setContentView(tv);
addContentView(tv.btnReset, tv.params);
}
public String getCurrDate()
{
String dt;
Date dNow = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddkkmmss");
dt = sdf.format(dNow);
return dt;
}
MyTouchEventView.java -
public class MyTouchEventView extends View {
private Paint paint = new Paint();
private Path path = new Path();
public Button btnReset;
public LayoutParams params;
public MyTouchEventView(Context context) {
super(context);
paint.setAntiAlias(true);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeWidth(10f);
btnReset = new Button(context);
btnReset.setText("Clear Screen");
params = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
btnReset.setLayoutParams(params);
btnReset.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
path.reset();
postInvalidate();
}
});
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.WHITE);
canvas.drawPath(path, paint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float pointX = event.getX();
float pointY = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
path.moveTo(pointX, pointY);
return true;
case MotionEvent.ACTION_MOVE:
path.lineTo(pointX, pointY);
break;
case MotionEvent.ACTION_UP:
break;
default:
return false;
}
postInvalidate();
return true;
}