Object versus Android Activity

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) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle presses on the action bar items
    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) {
            // TODO Auto-generated method stub

            // resets the screen
            path.reset();

            // Calls the onDraw() method
            postInvalidate();

        }
    });

}

@Override
protected void onDraw(Canvas canvas) {
    canvas.drawColor(Color.WHITE);
    canvas.drawPath(path, paint);
}

@Override
public boolean onTouchEvent(MotionEvent event) {

    // Gives you x and y coordinates on the Event.
    float pointX = event.getX();
    float pointY = event.getY();

    // Checks for the event that occurs
    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;
    }

    // Schedules a repaint.
    // Force a view to draw.
    postInvalidate();
    return true;
}
+3
3

, openTEV... ???? , , , .

, MainActivity BActivity. , openTEV, BActivity MainActivity. openTEV...

Intent intent = new Intent(MainActivity.this, BActivity.class);
startActivity(intent);

, BActivity, MainActivity.

+2

" ". canvas View, , . TextView, ImageView, EditText .. , .

.

 tv = new MyTouchEventView(this);
 setContentView(tv);

, "", , .

MyTouchEventView, , , , Intent. "", , MainActivity.

setContentView . , , - , .

http://developer.android.com/guide/components/tasks-and-back-stack.html

, . . a View - . A View . ViewGroup, LinearLayout, RelativeLayout .. XML-, . , .

, XML

http://developer.android.com/guide/topics/ui/custom-components.html

+1

Think if I’m not mistaken, but just doing the activity of the child. When creating a new action, you will get options to display one of the actions that you already have as the activity of the parent activity. Thus, in this way you will get a reverse navigation inaction panel, with which u can easily jump to ur parent activity from the child. ANd on mainActivity (parent) just use the intent to go to ur child activity.

Intent intent = new Intent(MainActivity.this, BActivity.class); //BActivity-the child activity
startActivity(intent);
0
source

All Articles